# \pgfmath and \foreach in TikZ

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?

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}


\documentclass[pstricks]{standalone}
\usepackage{pst-node,pst-plot}

\begin{document}
\curvepnodes[plotpoints=13]{0}{360}{2.5 t 90 add PtoC}{P}% 90 is an angular offset
\multido{\i=0+1}{\Pnodecount}{\Cnodeput(P\i){A\i}{\i}}
\Cnodeput(P\Pnodecount){A\Pnodecount}{}
\multido{\ix=0+1,\iy=1+1}{\Pnodecount}{\ncline{A\ix}{A\iy}}
\end{pspicture}
\end{document}


## Animation

\documentclass[pstricks]{standalone}
\usepackage{pst-node,pst-plot}

\begin{document}
\multido{\iN=4+1}{10}{%

• curvepnodes is defined in pst-node while plotpoints is defined in pst-plot. We have to load both packages when using curvepnodes, is it funny? – kiss my armpit Oct 29 '13 at 12:42