# How to tell TikZ to first compute and then display a variable?

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 \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 \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:

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.

-

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 \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:

-

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.

-

Just for fun with PSTricks.

\documentclass[pstricks,border=15pt,12pt]{standalone}
\usepackage{pst-node}
\degrees[5]

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!