# Tikz copying node handle

I am using foreach to draw a picture, and need the node handles for the previously drawn node and the current node. Precviously I used the following pattern to save separate coordinates:

\node (current) {first};

\foreach \i in {...} {
\coordinate (prev) at (current.west);
\node (current) {\i};

\draw[->] (prev) -- (current);
};

% There might be better solutions for this with chains or so,
% however, this is only to demonstrate the pattern


This becomes cumbersome rather quickly if one needs more than one coordinate from the node. Is there a method to copy the entire nodehandle to prev, so that prev.<anchor> (i.e. prev.west, prev.east) works?

MWE: As requested

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\begin{document}
\begin{tikzpicture}
{[every node/.style={state}]
\node (state) [initial] {$q_0$};
\path[->] (state) edge [loop below] ();
\foreach \i in {1,2,n} {
\coordinate (prev) at (state.east);
\node (state) [right=of prev] {$q_\i$};
\path[->] (state) edge [loop below] ();
\path[->] (prev) edge (state);
}
}
\end{tikzpicture}
\end{document}

• Please provide complete minimal working example (MWE). – Zarko Jun 24 '15 at 12:42
• I'm not sure as to what you're trying to achieve. Maybe posting some desired output might help. – Alenanno Jun 24 '15 at 12:48
• @Zarko: I added a MWE – ted Jun 24 '15 at 13:05
• @Alenanno: I would like to have access to the anchors of the node from the previous iteration of the foreach loop, without having to create a coordinate for every anchor. This is NOT about a desired output, it is a programming question: how to keep a node handle WITHOUT enumerating all the nodes drawn in the foreach loop with iteration specific names. – ted Jun 24 '15 at 13:07

There is \pgfnodealias, which can be used to copy the node data from one name to another:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\begin{document}
\begin{tikzpicture}
{[every node/.style={state}]
\node (state) [initial] {$q_0$};
\path[->] (state) edge [loop below] ();
\foreach \i in {1,2,n} {
\pgfnodealias{prev}{state}
\node (state) [right=of prev] {$q_\i$};
\path[->] (state) edge [loop below] ();
\path[->] (prev) edge (state);
}
}
\end{tikzpicture}
\end{document}


Not sure if this is what you need but you don't need to specify an anchor to draw a path in that fashion from one node to the other. This allows you to use variables instead of specifying anchors.

I added an extra foreach to show multiple foreach statements with different counts.

## Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata, positioning}

\tikzset{
every node/.style={state, draw=none}
}

\begin{document}
\begin{tikzpicture}
\foreach \n [count=\sft] in {3,1,5,7,2,9}{
\begin{scope}[yshift=\sft*2 cm]
\node (A) [initial] {$q_0$};
\path[->] (A) edge [loop below] ();
\foreach \i [remember=\i as \prev (initially A)] in {1,...,\n} {
\node (\i) [right =of \prev] {$q_\i$};
\path[->] (\i) edge [loop below] ();
\path[->] (\prev) edge (\i);
}
\end{scope}}
\end{tikzpicture}
\end{document}

• @MaartenDhondt Ahah, not even a full minute! Thanks. :D – Alenanno Jun 24 '15 at 13:59
• +1 for the remember bit. Its just that Heiko Oberdiek's answer takes the cake by doing exactly what I asked for. (Although this is definitely the better solution if I need to access the nodes again after the for loop) – ted Jun 24 '15 at 15:11
• @ted I see. I thought you wanted to reduce the number of commands so I used a way to remove the "coordinate" command altogether. :) – Alenanno Jun 24 '15 at 15:20
• @Alenanno: I think you understood me wrong: I relally like your answer, and actually used it, however, I can only accept one answer, and that has to be the other one, since that is what I would hope to find if I come here from a search engine. I *really* value your solution (I learned something very useful), my post was more intended as "I like your answer, but unfortunately I can accept only one." – ted Jun 24 '15 at 15:33
• @ted Yeah don't worry. :D No problem. I was just saying what I thought you needed. – Alenanno Jun 24 '15 at 15:34

Another example with foreach but with a pair of values denoting previous/present node

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{automata, positioning}
\begin{document}
\begin{tikzpicture}
{[every node/.style={state}]
\node (q0) [initial] {$q_0$};
\path[->] (q0) edge [loop below] ();
\foreach \i/\j in {0/1,1/2,2/n} {
\node (q\j) [right=of q\i] {$q_\j$};
\path[->] (q\j) edge [loop below] ();
\path[->] (q\i) edge (q\j);
}    }
\end{tikzpicture}
\end{document}


• This seems rather cumbersome/error prone compared to the other solutions. – ted Jun 24 '15 at 15:08