# tikz foreach loop alignment failure

 \documentclass[tikz]{standalone}

\usepackage{pgf,tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{figure}[t]
\begin{tikzpicture}[node distance=0.2em]

\node (T0) at (0,0) {$T_{0}$};
\node (T1) [below= of T0] {$T_{1}$};
\foreach \x in {2,3,...,8} {
\pgfmathsetmacro\result{\x-1}
\node (T\x) [below=of T\result] {$T_{\x}$};
}
\end{tikzpicture}
\end{figure}
\end{document}


Hi! In the above code I create 9 nodes. The first two which are out of the loop are perfectly aligned. The rest are not. Can someone suggest a solution so that all nodes are aligned? (Except for the obvious solution of copying and pasting).

I think the problem is in the positioning library because if I use \node (T\x.0) [below of = T\result] {$T_{\x}$}; instead then it appears correctly but I will have to adjust all node distances of by hand. The diagram includes many more nodes which I am not including for terms of simplicity. Below you can see the result of the code.

I'm not sure, if I understand your problem correctly, but nodes can be arranged in vertical line by aligned to center of nodes by:

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance=0.2em]
\node (T0) at (0,0) {$T_{0}$};
\foreach[count=\xi from 0]  \x in {1,2,...,8}
\node[below=of T\xi] (T\x)  {$T_{\x}$};
\end{tikzpicture}
\end{document}


You can use \pgfmathtruncatemacro instead of \pgfmathsetmacro. The problem is that \pgfmathsetmacro sets the macro to be a float; \pgfmathtruncatemacro truncates that to an integer.

On a side note, if you load tikz it is not necessary to explicitly load pgf, and moreover, for the standalone class, the [tikz] option means you don't have to load TikZ.

\documentclass[tikz]{standalone}

\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance=0.2em]

\node (T0) at (0,0) {$T_{0}$};
\node (T1) [below= of T0] {$T_{1}$};
\foreach \x in {2,3,...,8} {
\pgfmathtruncatemacro\result{\x-1}
\node (T\x) [below=of T\result] {$T_{\x}$};
}
\end{tikzpicture}
\end{document}

• @orestis: Zarko's answer is better IMHO. I will not be offended if you give the "accept" to him/her :-) – darthbith Jan 7 '15 at 20:25