Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want TikZ to first compute and then display the value of a variable. The question is, for sake of simplicity, based on this concrete example from TeXample.net:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def \n {5}
\def \radius {3cm}
\def \margin {8} % margin in angles, depends on the radius

\foreach \s in {1,...,\n}
{
  \node[draw, circle] at ({360/\n * (\s - 1)}:\radius) {$\s$};
  \draw[->, >=latex] ({360/\n * (\s - 1)+\margin}:\radius) 
    arc ({360/\n * (\s - 1)+\margin}:{360/\n * (\s)-\margin}:\radius);
}
\end{tikzpicture}
\end{document}

Suppose I messed up and I wanted my labels to run from 0 to n-1, instead of from 1 to n. How can I tell TikZ to first compute the difference or whatever operation on the variable, and then display it:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def \n {5}
\def \radius {3cm}
\def \margin {12} % margin in angles, depends on the radius

\foreach \s in {1,...,\n}
{
  \node[draw, circle] at ({360/\n * (\s - 1)}:\radius) {$\s-1$};
  \draw[->, >=latex] ({360/\n * (\s - 1)+\margin}:\radius) 
    arc ({360/\n * (\s - 1)+\margin}:{360/\n * (\s)-\margin}:\radius);
}
\end{tikzpicture}
\end{document}

I even used a temporary auxiliary variable \r=\s-1 but it still prints the following instead the nodes' numbers from 0 to n-1: enter image description here

Remark: I know the problem would be solved by defining another range for \s, i.e. {0,...,\n-1} but I already wrote a similar code (but long, so it wouldn't be a MWE if I post it) based on this, with dependence on the \s and I wouldn't like to shift lot of times the variable to \s+1 in the somehow long formulas.

share|improve this question

3 Answers 3

up vote 15 down vote accepted

To number objects from 0 up to value n-1, it is possible to use evaluate or count keys. Both are illustrated in the pgfmanual while explaining foreach operation. An example with the latter:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def \n {5}
\def \radius {3cm}
\def \margin {12} % margin in angles, depends on the radius

\foreach \s[count=\xi from 0] in {1,...,\n}
{
  \node[draw, circle] at ({360/\n * (\s - 1)}:\radius) {$\xi$};
  \draw[->, >=latex] ({360/\n * (\s - 1)+\margin}:\radius) 
    arc ({360/\n * (\s - 1)+\margin}:{360/\n * (\s)-\margin}:\radius);
}
\end{tikzpicture}
\end{document}

The result:

enter image description here

share|improve this answer

Just for fun with PSTricks.

\documentclass[pstricks,border=15pt,12pt]{standalone}
\usepackage{pst-node}
\degrees[5]
\psset{radius=12pt,arcsep=36pt,arrows=->}
\begin{document}
\begin{pspicture}[showgrid=false](-4,-4)(4,4)
    \foreach \i in {1,2,...,5}{\rput(4;\the\numexpr\i-1){\Circlenode{N\i}{\the\numexpr\i-1}}}
    \foreach \i in {1,2,...,5}{\psarc(0,0){4}{(N\i)}{(N\the\numexpr\i+1)}}
\end{pspicture}
\end{document}

enter image description here

Notes

Consider \foreach \i in {1,2,...,5}{\psarc(0,0){4}{(N\i)}{(N\the\numexpr\i+1)}}. If \i=5 then \psarc(0,0){4}{(N5)}{(N6)} but N6 does not exist and no error is generated. PSTricks is so smart!

share|improve this answer

Complement to Claudio's answer without additional variables, for not so complicated arithmetic, you can use eTeX's \numexpr but to make it work in the math mode in terms of expansion, you need to add \number in front of it.

{$\number\numexpr\s-1\relax$};

For the remaining parts you can do the math computation via pgfmath engine.

 {\pgfmathparse{int(\s-1)}\pgfmathresult};

Both give the same result as Claudio's answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.