2

Why can't I draw lines between tikz nodes generated by loops with multiple variables?

I have a bunch of nodes, some of which I want to connect with a sequence of arrows. The notes are numbered, and I've tried to store some more data as well; in my minimal example below I've stored coordinates.

I found a way to create the notes. My problem is that I can't seem to use them to draw. The following example compiles:

\begin{tikzpicture}
\foreach \k in {0,1,2} 
\node (\k) at (\k,\k*\k-2*\k) {\k};
\def\Data{
  3 / 2  / 1,
  4 / 1  / 2,
  5 / 2  / 3}
\foreach \a / \x / \y in \Data 
\node (\a) at (\x,\y) {\a};

\draw (0) -- (2);
%\draw (3) -- (4);
\foreach \note  [remember=\note as \lastnote (initially {(0)})] in {(1),(2)} \draw[->] \lastnote -- \note;
% \foreach \note  [remember=\note as \lastnote (initially {(3)})] in {(4),(5)} \draw[->] \lastnote -- \note;
\end{tikzpicture}

If I uncomment any of the two commented lines, I get an error message that "Package pgf Error: No shape named 3 is known."

How are the nodes 0, 1, 2 different from the nodes 3, 4, 5? Why can't I draw with the latter?

4

When you make the list with e.g. 3 / 2 / 1, the space is actually included, and it becomes included in the node name as well. Hence, if you were to do \draw (3 ) -- (4 );, note (X<space>), it works. Probably better to define the list without the spaces in the first place, i.e. 3/2/1.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\foreach \k in {0,1,2} 
\node (\k) at (\k,\k*\k-2*\k) {\k};
\def\Data{
  3/2/1,
  4/1/2,
  5/2/3}
\foreach \a/\x/\y in \Data 
\node (\a) at (\x,\y) {\a};

\draw (0) -- (2);
\draw (3) -- (4);
\foreach \note  [remember=\note as \lastnote (initially {(0)})] in {(1),(2)} \draw[->] \lastnote -- \note;
\foreach \note  [remember=\note as \lastnote (initially {(3)})] in {(4),(5)} \draw[->] \lastnote -- \note;
\end{tikzpicture}

\end{document}

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.