5

In tikZ's manual, following code is proposed to illustrate usage of foreach inside a path construction command:

enter image description here

On the image below, this is figure (1). Strangely, when the starting point ((0,0) in the above example) is replaced by a named node (figure (2)), the result is completely different : the paths constructed in the loop no longer follow each other, they all start from the given node. Starting from named coordinates (figure (3)) again results in the wanted bahaviour.

enter image description here

\documentclass[margin=1cm]{standalone}
\usepackage{tikz}

\begin{document}
(1) \tikz \draw (0,0) foreach \x in {1,...,3} { -- (\x,1) -- (\x,0) };
(2) \tikz {\node (a) at (0,0) {}; \draw (a) foreach \x in {1,...,3} { -- (\x,1) -- (\x,0) };}
(3) \tikz {\coordinate (a) at (0,0); \draw (a) foreach \x in {1,...,3} { -- (\x,1) -- (\x,0) };}
\end{document}

I couldn't find anything about that in the manual. Maybe this cryptic line?

Note that the node and pic path commands also support the foreach statement in special ways.

Now what should I do to be able to use such a construct but starting from a node?

Note : pgffor.sty 2020/01/08 v3.1.5b (3.1.5b)

3
  • use a dedicted coordinate from the node, e.g. \draw (a.center) Commented Apr 27, 2021 at 14:41
  • I can see the difference that makes when using (a.center) instead of just a but I still wonder why the foreach loop doesn't provide the same saw line. If someone could explain...
    – SebGlav
    Commented Apr 27, 2021 at 22:17
  • @UlrikeFischer I could do that, but if the starting node has content, the result would be different. I would actually have to find the precise anchor that tikZ would use when drawing between nodes. In the situation above, this is easy (a.east) but in general, it is not. Commented Apr 28, 2021 at 10:23

1 Answer 1

1

You can look at Expansion of foreach loop inside \draw also. Basically, there are two things:

  1. the movements of an in-line foreach act totally in a local way, as they were issued each one in a {[current point is local] ... } group. That means that the "current position" is reset at every loop.

  2. When drawing a line starting from a node, TikZ uses the border anchor if it's available, so the line will not start from the center. You can retrieve the position using coordinate[pos=0] when the path is built (see below), but I do not know how to integrate that into your foreach code without a lot of gimmicks...

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[]
    \node [circle, draw] (a) at (0,0) {Node};
    \draw (a) coordinate(naked-a) -- ++(1,2)
        coordinate[pos=0](border-a)
        coordinate[pos=1](final);
    \node [fill=red, circle, inner sep=2pt] at (naked-a) {}; 
    \node [fill=blue, circle, inner sep=2pt] at (border-a) {}; 
    \node [fill=green, circle, inner sep=2pt] at (final) {}; 
\end{tikzpicture}
\end{document}

output from the code above

You must log in to answer this question.

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