[Tex/LaTex] \pgfmath and \foreach in TikZ

foreachpgfmathtikz-pgf

I am quite new to TikZ. I have the following code:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain=circle placed {at=(\tikzchaincount*30:1.5)},
                    regular/.style={draw,circle,inner sep=0,minimum size=4mm}]
\foreach \i in {0,...,11}
  \node [on chain, regular] (\i) {\i};
\foreach \i in {0,...,11}
  \draw [->] (\i) to ({\pgfmathparse{int(mod(\i+1,12))}\pgfmathresult});
\end{tikzpicture}
\end{document}

What I intended to do is to draw a series of 12 nodes evenly placed on a circle, and draw an arrow between every two neighbors. LaTeX gave an error message saying

!incomplete \iffalse; all text was ignored after ...

What is the correct way to do what I intended?

Best Answer

When using macros in node names, the macros have to be expandable in an \edef context. \pgfmathparse is not. So you need to do the computation beforehand and only use the result of it in the node name. One way is to use the evaluate key on the \foreach as in the following.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/141259/86}
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[start chain=circle placed {at=(\tikzchaincount*30:1.5)},regular/.style={draw,circle,inner sep=0,minimum size=4mm}]
\foreach \i in {0,...,11}
  \node [on chain, regular] (\i) {\i};
\foreach[evaluate=\i as \ni using {int(mod(\i+1,12))}] \i in {0,...,11}
  \draw [->] (\i) to (\ni);
\end{tikzpicture}
\end{document}

circular nodes