13

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?

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
wdg
  • 739
  • 1
  • 6
  • 12

2 Answers2

18

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

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
6
\documentclass[pstricks]{standalone}
\usepackage{pst-node,pst-plot}

\begin{document}
\begin{pspicture}[arrows=->,radius=12pt](-3,-3)(3,3)
    \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}

enter image description here

Animation

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

\begin{document}
\multido{\iN=4+1}{10}{%
\begin{pspicture}[arrows=->,radius=12pt](-3,-3)(3,3)
    \curvepnodes[plotpoints=\iN]{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}

enter image description here

  • 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