# tikz node name and calculation

I can't find out why the following code :

\documentclass[border=0mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.multipart, fit,backgrounds,calc}

\begin{document}
\begin{tikzpicture}
\node(poste1){poste};
\foreach\x in {1,...,5}{%
\pgfmathparse{\x+1}
\node[right=of poste\x](poste\pgfmathresult){poste\pgfmathresult};
}
\end{tikzpicture}


doesn't work. With this code

\documentclass[border=0mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.multipart, fit,backgrounds,calc}

\begin{document}
\begin{tikzpicture}
\node(poste1){poste};
\foreach\x in {1}{%
\pgfmathparse{\x+1}
\node[right=of poste\x](poste\pgfmathresult){poste\pgfmathresult};
}

\end{tikzpicture}
\end{document}

\end{document}


it appears that the \pgfmathresult command return a wrong number hence the error but I don't know why this occurs. Could somebody help me please?

• \pgfmathresult probably returns something like 1.9999997 instead of 2. Have you tried \pgfmathparse{int(\x+1)}. If that also fails, set an integer macro and use it's value: \pgfmathtruncatemacro\myNext{\x+1} \node[right=of poste\x](poste\myNext){poste\myNext}; – Tom Bombadil Nov 19 '15 at 20:22
• @Tom, can you transform your comment in answer? I like to see, how to use \pgfmathtruncatemacro\myNext{\x+1}, as \pgfmathparse{int(\x+1)} fails in all my tests. – Zarko Nov 19 '15 at 21:39
• In your first listing, simply replace \pgfmathparse{\x+1} \node[right=of poste\x](poste\pgfmathresult){poste\pgfmathresult}; with \pgfmathtruncatemacro{\myNext}{\x+1} \node[right=of poste\x](poste\myNext){poste\myNext}; – Tom Bombadil Nov 19 '15 at 21:42

Simpler than use of \pgfmathparse is:

\documentclass[border=0mm,tikz]{standalone}
\usetikzlibrary{%backgrounds,calc,fit,
positioning,
%shapes.multipart
}

\begin{document}
\begin{tikzpicture}[node distance= 2mm]
\node (poste1) {poste1};
\foreach \x [count=\xx from 2] in {1,2,...,5}
{
\node[right=of poste\x] (poste\xx) {poste \xx};
}
\end{tikzpicture}
\end{document}


You can use a simple evaluate= as this:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.multipart, fit,backgrounds,calc}
\begin{document}

\begin{tikzpicture}
\node(poste1){poste1};
\foreach \x [evaluate=\x as \y using int(\x-1)] in {2,...,6}{%
\node[right=of poste\y](poste\x){poste\x};
}
\end{tikzpicture}

\end{document}


which gives the correct result:

The reasons why your example doesn't work are as follows:

1. The \pgfmathparse command evaluates to a floating point number (2.0, 3.0, etc).

2. You should use the \pgfmathresult immediately after its \pgfmathparse.

3. For reason 2, you can define a new macro \pgfmathtruncatemacro\y{\x+1}, as suggested by @TomBombadil in a comment, and replace each \pgfmathresult with \y.

• Do you think it altogether fair to edit your answer an hour after another was posted to use the method from that answer without even crediting that answer? You were using evaluate and the other answer count which is simpler. An hour later, you changed yours to use count without acknowledging that this method was used by the other answer. – cfr Nov 19 '15 at 22:35
• @cfr - Here is the original solution again. BTW, I didn't try to copy the other answer, I almost forgot that the count= method was used above (and I used it differently) and I do acknowledge other people (you see, I mentioned TomBombadil when I talked about his idea). – AboAmmar Nov 19 '15 at 22:57
• You could use your version of the count code and just link to the other answer as similar. I didn't mean you were copying, but just that, since the other answer was posted first and this is the same question, I think it is courteous/fairer to explicitly acknowledge that. It may well just have been an oversight on your part - that is extremely easy to do, goodness knows, and I do it myself. – cfr Nov 19 '15 at 23:26
• I think you should make clear what immediately after means. The OP may think that's what he/she is doing. I'd phrase it as, "save \pgfmathresult to command control sequence before doing anything else in TikZ". – A.Ellett Nov 20 '15 at 0:10