3

I would like to apply yshift effect to nodes defined with foreach loop. In below example,the output line not shift as expected:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
    box/.style={circle,draw,inner sep=1}
]
\def\arr{
  A/0,
  B/22mm,
  C/16mm
  }
\def\dy{4mm}
\foreach \name/\offset [count=\i from 0,evaluate=\i as \j using {int(\i-1)}] in \arr {
  \ifnum\i=0
    \node[box] (P\i) {\name};
  \else
      \node[box,right=\offset of P\j] (P\i) {\name};
  \fi
  }
\draw[-angle 90] ([yshift=\dy]P0) -- ([yshift=\dy]P2) node[pos=.5,sloped,above] {direction};
\end{tikzpicture}

\end{document}

Results: enter image description here

0

2 Answers 2

4

If you specify .north, the line is shifted above the tops of the nodes.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
box/.style={circle,draw,inner sep=1}
]
\def\arr{
A/0,
B/22mm,
C/16mm
}
\def\dy{4mm}
\foreach \name/\offset [count=\i from 0,evaluate=\i as \j using {int(\i-1)}] in \arr {
\ifnum\i=0
\node[box] (P\i) {\name};
\else
\node[box,right=\offset of P\j] (P\i) {\name};
\fi
}
\draw[-angle 90] ([yshift=\dy]P0.north) -- ([yshift=\dy]P2.north) node[pos=.5,sloped,above] {direction};
\end{tikzpicture}

\end{document}

line shifted 4mm above tops of referenced nodes

9
  • but I'm not sure why the line doesn't seem to be shifted at all in the original example. shouldn't it be shifted from .center by default?
    – cfr
    Commented Aug 11 at 19:38
  • 1
    Fun fact: using any explicit position works, e.g. P0.center would do as well. Commented Aug 11 at 19:40
  • @samcarter_is_at_topanswers.xyz but why?
    – cfr
    Commented Aug 11 at 22:08
  • I guess this bypasses this "calculate the point closest(?) to the other node on the edge"-thingy of tikz...I forgot the details how exactly tikz determines the start point on the edge of the node. Commented Aug 11 at 22:11
  • 1
    @samcarter_is_at_topanswers.xyz Indeed, when the start coordinate is a non-coordinate node, TikZ notes down that there still might be a “move to waiting” (to the border of the node) for many of the path operations. The transformed point will be just thrown away in favor of the proper point on the border. Commented Aug 12 at 8:00
4

You could use transform canvas={yshift=\dy} to shift the whole line instead of just the coordinates:

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
    box/.style={circle,draw,inner sep=1}
]
\def\arr{
  A/0,
  B/22mm,
  C/16mm
  }
\def\dy{4mm}
\foreach \name/\offset [count=\i from 0,evaluate=\i as \j using {int(\i-1)}] in \arr {
  \ifnum\i=0
    \node[box] (P\i) {\name};
  \else
      \node[box,right=\offset of P\j] (P\i) {\name};
  \fi
  }
\draw[transform canvas={yshift=\dy},-angle 90] (P0) -- (P2) node[pos=.5,sloped,above] {direction};
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .