# Tikz: Division and Addition for Node Names

I am trying to draw a graph that is essentially just a long line with a triangle at one end and a square at the other end. I need there to be a chain of length 26 between the triangle and the square. When I tried with the code below, I got the error "Missing Number, treated as 0". Other attempts have told me that there are no nodes named "P0+1". How do I do division, addition, etc. on node names to iterate through them as I'd like?

\begin{tikzpicture}
\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12}{
\node (P\x) at (-\x/2,0){};
}
\foreach \x in {13,14,15,16,17,18,19,20,21,22,23,24,25,26}{
\node (P\x) at ({\x-13}/2,0){};
}

\node (P27) at (14,1){};
\node (P28) at (14,-1){};
\node (P29) at (-14,1){};
\node (P30) at (-14,-1){};
\node (P31) at (-15,0){};

\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}{
\fill (P\x) circle (2pt);
}

\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}{
\draw[line width=1 pt] (P\x)--(P\x+1);
}

\draw[line width=1 pt] (P26)--(P27);
\draw[line width=1 pt] (P27)--(P28);
\draw[line width=1 pt] (P28) -- (P26);
\draw[line width=1 pt] (P13)--(P29);
\draw[line width=1 pt] (P29)--(P30);
\draw[line width=1 pt] (P30) -- (P31);
\draw[line width=1 pt] (P31) -- (P13);

\draw (0,-3) node[below]{Figure 3.4};
\end{tikzpicture}
• You are referring to the nodes named P1+1, P2+1, and so on (not P2, P3, etc). Try using \foreach \x [evaluate={\y=int(\x+1);}] in and then using the coordinates (P\x) -- (P\y). Dec 19, 2013 at 9:45

There are certain places where TikZ don't parse the math such as node names and a few other places. Inside a loop you can instead use the evaluate=<var> as <resulting var> using <formula> syntax. You could have used a counter that starts from 1 in this specific case too.

Moreover you need to use (...) to nest the math and {...} to hide it from the parser. Hence the code becomes

\begin{tikzpicture}
\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12}{
\node (P\x) at (-\x/2,0){};
}
\foreach \x in {13,14,15,16,17,18,19,20,21,22,23,24,25,26}{
\node (P\x) at ({(\x-13)/2)},0){};
}

\node (P27) at (14,1){};
\node (P28) at (14,-1){};
\node (P29) at (-14,1){};
\node (P30) at (-14,-1){};
\node (P31) at (-15,0){};

\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}{
\fill (P\x) circle (2pt);
}

\foreach \x[evaluate=\x as \evalx using int(\x+1)] in {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}{
\draw[line width=1 pt] (P\x)--(P\evalx);
}

\draw[line width=1 pt] (P26)--(P27);
\draw[line width=1 pt] (P27)--(P28);
\draw[line width=1 pt] (P28) -- (P26);
\draw[line width=1 pt] (P13)--(P29);
\draw[line width=1 pt] (P29)--(P30);
\draw[line width=1 pt] (P30) -- (P31);
\draw[line width=1 pt] (P31) -- (P13);

\draw (0,-3) node[below]{Figure 3.4};
\end{tikzpicture}

• Can I use multiple variables in the formula? Dec 11, 2020 at 17:21