I want to place a variable number of nodes on a page, but I also want to be able to give these nodes a (integer) name. However, I run into the problem that only certain kind of commands can appear in the label of a node (see this question).
My naive attempt to achieve what I want is...
\documentclass{article}
\usepackage{tikz}
\def\n{3}
\begin{document}
\begin{tikzpicture}
\node[draw, circle] (\n + 1) {4};
\node[draw, circle] (\n + 2) [right of=4] {5};
\end{tikzpicture}
\end{document}
...but this gives the error message...
! Package pgf Error: No shape named 4 is known.
...since the name of the node is actually "3 + 1" (or something close to it).
I found this hack...
\documentclass{article}
\usepackage{tikz}
\def\n{3}
\begin{document}
\begin{tikzpicture}
\pgfmathtruncatemacro{\nPlusOne}{\n + 1}
\node[draw, circle] (\nPlusOne) {4};
\node[draw, circle] (\n + 2) [right of=4] {5};
\end{tikzpicture}
\end{document}
...which has the right behavior. However, when calling nested functions that also use this hack, they redefine \nPlusOne
using their given value of \n
.
Question
What is a better way to assigned a computed integer to the label of a node?
\foreach
statement? – Claudio Fiandrino Jul 25 '14 at 13:35