我希望使用 yshift 根据评估结果的偏移量绘制一些线条:
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\def\dx{2cm}
\def\dy{2}
\node (L1) {L1};
\node[right=\dx of L1] (L2) {L2};
\node[right=\dx of L2] (L3) {L3};
\foreach \from/\to/\desc [count=\i from 1,evaluate=\i as \y using {-\i*\dy} ] in {
L1/L2/A,
L2/L3/B,
L3/L1/C
} {
\draw[->] ([yshift=\y]\from) -- ([yshift=\y]\to) node[midway,label=above:\desc] (N\i) {};
}
\foreach \x in {L1,L2,L3} {
\draw[line width=2pt] (\x) -- (\x |- N\i);
}
\end{tikzpicture}
\end{document}
但听起来 yshfit 行为不正常:
答案1
这是朝着正确的方向发展吗?我试图从字里行间看出其中的道理。
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\def\dx{2cm}
\def\dy{2pt}
\node (L1) {L1};
\node[right=\dx of L1] (L2) {L2};
\node[right=\dx of L2] (L3) {L3};
\foreach \from/\to/\desc [count=\i from 0,evaluate=\i as \y using {7-\i*\dy} ] in {
L1/L2/A,
L2/L3/B,
L3/L1/C
} {
\draw[-stealth,shorten >=1pt] ([yshift=\y]\from.north) -- ([yshift=\y]\to.north) node[midway,label=above:\desc] (N\i) {};
}
\foreach \x [count=\i from 0] in {L1,L2,L3} {
\draw[line width=2pt,shorten >=-1.5pt] (\x) -- (\x |- N\ifnum\i=0 \i\else\the\numexpr\i-1\fi);
}
\end{tikzpicture}
\end{document}
答案2
shift 命令用于坐标,而不是节点。薛定谔的猫的答案很棒。我猜这是用于简单的 UML 序列图绘制。添加一个具有更多更好输出的新版本:
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\def\dx{2cm}
\def\dy{0.5cm}
\node (L1) {L1};
\node[right=\dx of L1] (L2) {L2};
\node[right=\dx of L2] (L3) {L3};
\foreach \from/\to/\desc [count=\i from 1,evaluate=\i as \y using {-\i*\dy} ] in {
L1/L2/A,
L2/L3/B,
L3/L1/C
} {
\draw[-stealth,shorten >=1pt] ([yshift=\y]\from.south) -- ([yshift=\y]\to.south) node[midway,label=above:\desc] (N\i) {};
}
\foreach \x in {L1,L2,L3} {
\draw[line width=2pt,shorten >=-2pt] (\x) -- (\x |- N\i);
}
\end{tikzpicture}
\end{document}