# Drawing an array of nodes using the foreach-construct

I sometimes need to visualize an array-like structure with tikz. I use nodes for array elements and position the next node right of the previous one, i.e., like this:

 \node[draw,rectangle] (n0) {element 1};
\node[draw,rectangle] (n1) [right of=n0] {element 2};
\node[draw,rectangle] (n2) [right of=n1] {element 3};
% many more nodes


In other words: I always position the next node right of the previous node. I want to simplify that code, e.g., by using a loop, and ideally would like to have something like this:

\foreach \id in {1,...,9}
\draw let \p0 = {\id-1} in node[draw,rectangle] (n\id) {element \id};


There is two issues with this:

• The clause {\id-1} is not "evaluated", but taken as a string, i.e., "1-1", "2-1", etc. How could I do that? Or, if that is not possible or convenient, can I somehow specify right of = latest defined id?

• The first element is special in the sense that it is not relative to any other node. Special cases are always tedious, so I'm wondering if there is a way to define right of = none? It'd be nice to draw all nodes with the loop and not having to treat the first element as a special case.

## 3 Answers

Another version of evaluate uses remember.

## Code

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

\begin{document}
\begin{tikzpicture}

\node[draw, rectangle] (n0) {0};
\foreach \x [remember=\x as \lastx (initially 0)] in {1,...,4}{
\node [draw, rectangle, right =of n\lastx] (n\x) {\x};
}
\end{tikzpicture}
\end{document}

• Or also (and shorter), using e-TeX's \numexpr as in \coordinate (n0); \foreach \id in {1,...,9} { \node[draw,rectangle,right=of n\the\numexpr\id-1\relax] (n\id) {\id}; } – Gonzalo Medina Jul 10 '15 at 14:21
• @GonzaloMedina Cool. :) I still need to learn all of those things. But I don't know where everybody learns them from. Like \the, \numexpr, are they in the Tex Book? – Alenanno Jul 10 '15 at 14:22
• \the is a TeX primitive, so it's on The TeXbook. \numexpr is from e-TeX, so it's not in the The TeXbook, but on etex documentation. – Gonzalo Medina Jul 10 '15 at 14:25
• @GonzaloMedina Thanks for the advice. I'll check it out. :) – Alenanno Jul 10 '15 at 14:25

This is what chains library is for and it automatically names the nodes of your preference. Also you can automatically join them, change directions etc.

\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}
\begin{scope}[start chain=my chain]
\foreach \i in {0,...,6}{\node [on chain] {element \i};}
\end{scope}
\draw[->] (my chain-2) to[bend left] (my chain-5);
\end{tikzpicture}
\end{document}


You can have an anchor \coordinate with id 0. Then, there are at least three similar ways to do what you want.

1. Maintain the name of the last node yourself.

\coordinate (n0);
\xdef\mylastnode{0}
\foreach \id in {1,...,9} {
\node[draw,rectangle,right=of n\mylastnode] (n\id) {\id};
\xdef\mylastnode{\id}
}


This is more flexible than just compute i - 1.

2. In simple cases such as this (numbered nodes), do the numbers:

\coordinate (n0);
\foreach \id in {1,...,9} {
\pgfmathparse{\id-1}
\node[draw,rectangle,right=of n\pgfmathresult] (n\id) {\id};
}

3. You can also let foreach do the computation.

\coordinate (n0);
\foreach \id [evaluate=\id as \last using \id-1] in {1,...,9} {
\node[draw,rectangle,right=of n\last] (n\id) {\id};
}

• Or also (and shorter), using e-TeX \numexpr as in \coordinate (n0); \foreach \id in {1,...,9} { \node[draw,rectangle,right=of n\the\numexpr\id-1\relax] (n\id) {\id}; } – Gonzalo Medina Jul 10 '15 at 14:08