我很难将文本定位在我在 TikZ 中绘制的路径上方或下方。
以下是一个例子:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{
arrows,
scopes,
decorations.pathmorphing,
decorations.markings}
\begin{document}
\tikzstyle{doublepath}=
[draw, line width=2mm, shorten >=4.9mm, shorten <=4.9mm,
decoration={markings,
mark=at position 0 with {\arrow[line width=1mm, scale=-1]{triangle 45}},
mark=at position .999 with {\arrow[line width=1mm]{triangle 45}}},
postaction={decorate}]
\tikzstyle{strength}=[midway,thin,inner sep=0,outer sep=0,sloped]
\begin{tikzpicture}[font=\sffamily]
\path node[align=center] (A) {A};
\path (0,6) node[align=center] (B) {B};
\path (6,0) node[align=center] (C) {C};
\path (6,6) node[align=center] (D) {D};
\draw[doublepath,in=270-20,out=90+20] (A) to node [strength,below=2.36mm] {Label 1} (B) ;
\draw[doublepath,in=270+20,out=90-20] (A) to node [strength,below=2.36mm] {Label 2} (B) ;
\draw[doublepath,in=270-20,out=90+20] (C) to node [strength,below=2.36mm,pos=.49] {Label 1} (D) ;
\draw[doublepath,in=270+20,out=90-20] (C) to node [strength,below=2.36mm,pos=.49] {Label 2} (D) ;
\draw[doublepath,in=270+20,out=90-20] (C) to node [strength,below=2.36mm,pos=.51] {Label 3} (D) ;
\draw[doublepath,in=180-20,out=0+20] (A) to node [strength,below=2.36mm,pos=.49] {Label 4} (C) ;
\draw[doublepath,in=180-20,out=0+20] (A) to node [strength,below=2.36mm,pos=.51] {Label 5} (C) ;
\draw[doublepath,in=180+20,out=0-20] (A) to node [strength,below=2.36mm] {Label 6} (C) ;
\end{tikzpicture}
\end{document}
我遇到的问题是“上方”和“下方”似乎是指文档(上方 = 朝向顶部,下方 = 朝向底部)而不是所绘制路径的左侧或右侧。
对于垂直路径,上方和下方似乎不再明确。如果路径像示例中那样弯曲,位置的轻微移动将使节点文本从一侧翻转到另一侧(示例中的标签 2 和 3 分别位于位置 0.49 和 0.51)。我理解其中的逻辑,但这无助于解决我的问题。有选项 left= 和 right=,但这些选项将以不同的方式锚定节点并沿路径移动标签。
(一个相关的问题是,是否可以让文本始终以基线在左,例如,无论它是在路径的上方还是下方,相对于路径的方向。我尝试了 rotate=180,但这也会改变与路径的距离,因为锚点不在中间)。
我该如何解决这个问题?理想情况下,我希望能够将文本放置在垂直路径的左侧或右侧,或者指定文本相对于路径方向的一侧。此外,如果能够旋转文本,以便所有文本都可以从同一侧读取,无论文本是在路径上方还是下方,那就太好了。我很乐意做一些微调;但是,目前这真的很尴尬,因为只要我改变以垂直为主的路径弯曲的方向或程度,标签可能会翻转到另一侧。
答案1
钛钾Z 尝试智能地解释above
和below
。如果您觉得这令人困惑,请添加键allow upside down
,然后below
将始终表示路径的同一侧(右侧)。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{
arrows,
scopes,
decorations.pathmorphing,
decorations.markings}
\begin{document}
\tikzset{doublepath/.style={draw, line width=2mm, shorten >=4.9mm, shorten <=4.9mm,
decoration={markings,
mark=at position 0 with {\arrow[line width=1mm, scale=-1]{triangle 45}},
mark=at position .999 with {\arrow[line width=1mm]{triangle 45}}},
postaction={decorate}},
strength/.style={midway,thin,inner sep=0,outer sep=0,sloped}}
\begin{tikzpicture}[font=\sffamily,allow upside down]
\path node[align=center] (A) {A};
\path (0,6) node[align=center] (B) {B};
\path (6,0) node[align=center] (C) {C};
\path (6,6) node[align=center] (D) {D};
\draw[doublepath,in=270-20,out=90+20] (A) to node [strength,below=2.36mm] {Label 1} (B) ;
\draw[doublepath,in=270+20,out=90-20] (A) to node [strength,below=2.36mm] {Label 2} (B) ;
\draw[doublepath,in=270-20,out=90+20] (C) to node [strength,below=2.36mm,pos=.49] {Label 1} (D) ;
\draw[doublepath,in=270+20,out=90-20] (C) to node [strength,below=2.36mm,pos=.49] {Label 2} (D) ;
\draw[doublepath,in=270+20,out=90-20] (C) to node [strength,below=2.36mm,pos=.51] {Label 3} (D) ;
\draw[doublepath,in=180-20,out=0+20] (A) to node [strength,below=2.36mm,pos=.49] {Label 4} (C) ;
\draw[doublepath,in=180-20,out=0+20] (A) to node [strength,below=2.36mm,pos=.51] {Label 5} (C) ;
\draw[doublepath,in=180+20,out=0-20] (A) to node [strength,below=2.36mm] {Label 6} (C) ;
\end{tikzpicture}
\end{document}
有了这些信息,就可以更容易地避免碰撞。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{
arrows,
scopes,
decorations.pathmorphing,
decorations.markings}
\begin{document}
\tikzset{doublepath/.style={draw, line width=2mm, shorten >=4.9mm, shorten <=4.9mm,
decoration={markings,
mark=at position 0 with {\arrow[line width=1mm, scale=-1]{triangle 45}},
mark=at position .999 with {\arrow[line width=1mm]{triangle 45}}},
postaction={decorate}},
strength/.style={midway,thin,inner sep=0,outer sep=0,sloped}}
\begin{tikzpicture}[font=\sffamily,allow upside down]
\path node[align=center] (A) {A};
\path (0,6) node[align=center] (B) {B};
\path (6,0) node[align=center] (C) {C};
\path (6,6) node[align=center] (D) {D};
\draw[doublepath,in=270-20,out=90+20] (A) to node [strength,below=2.36mm] {Label 1} (B) ;
\draw[doublepath,in=270+20,out=90-20] (A) to node [strength,below=2.36mm] {Label 2} (B) ;
\draw[doublepath,in=270-20,out=90+20] (C) to node [strength,below=2.36mm,pos=.49] {Label 1} (D) ;
\draw[doublepath,in=270+20,out=90-20] (C) to node [strength,below=2.36mm,pos=.49] {Label 2} (D) ;
\draw[doublepath,in=270+20,out=90-20] (C) to node [strength,above=2.36mm,pos=.51] {Label 3} (D) ;
\draw[doublepath,in=180-20,out=0+20] (A) to node [strength,above=2.36mm,pos=.49] {Label 4} (C) ;
\draw[doublepath,in=180-20,out=0+20] (A) to node [strength,below=2.36mm,pos=.51] {Label 5} (C) ;
\draw[doublepath,in=180+20,out=0-20] (A) to node [strength,below=2.36mm] {Label 6} (C) ;
\end{tikzpicture}
\end{document}
请注意,\tikzstyle
已被弃用。
答案2
带有bend left
箭头bend right
并使用quotes
包装的边缘标签:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
positioning,
quotes,
}
\begin{document}
\begin{tikzpicture}[
node distance = 6cm,
every edge/.style = {draw, line width=2mm,
{Triangle[length=5mm, width=5mm]}-{Triangle[length=5mm, width=5mm]},
bend angle=20, shorten >=2mm, shorten <=2mm
},
every edge quotes/.style = {inner sep=2mm, font = \sffamily, auto=right, sloped},
]
\node (A) {A};
\node (B) [above=of A] {B};
\node (C) [right=of A] {C};
\node (D) [above=of C] {D};
%
\draw (A) edge [bend left, "Label 1"] (B)
(A) edge [bend right, "Label 2"] (B)
(A) edge [bend left, "Label 5","Label 4" '] (C)
(A) edge [bend right, "Label 6"] (C)
(C) edge [bend left, "Label 1" '] (D)
(C) edge [bend right, "Label 2","Label 3" '] (D);
\end{tikzpicture}
\end{document}
其中"Label 3 '
相当于"Label 3" swap
。这将标签 3 移动到边缘的另一侧: