3

I wish to use yshift to draw some lines by a offset from evaluate result:

\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}

But sounds like the yshfit behavior is abnormal:

enter image description here

  • \dy does not have units, so the shift is interpreted in points, and it would be cleaner to apply the shift to an anchor, e.g. \draw[->] ([yshift=\y]\from.south) -- ([yshift=\y]\to.south) node[midway,label=above:\desc] (N\i) {};. BTW, \foreach \x in {L1,L2,L3} { misses a [count=\i from 0], so should really be \foreach \x [count=\i from 0] in {L1,L2,L3} {, or something that defines \i. Also I do not quite see what you wish to achieve. – user194703 Nov 4 '19 at 17:30
2

Does this go in the right direction? I was trying to read between the lines.

\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}

enter image description here

| improve this answer | |
  • Great, that's what I want if change dy from 2pt to 2cm. we should use anchor to fix this issue. – lucky1928 Nov 4 '19 at 17:55
1

shift command is for coordinate, not for node. Schrödinger's cat's answer is awesome.I guess it's for a simple UML sequence diagram drawings. Add a new version with more better output:

\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}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.