# Redefining variables in a \foreach loop

I'm trying to plot some recursively defined sequence with tikz and am encountering some behavior I can't understand, here is my code:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\f}{1}
\foreach\i in {1,...,10} {
\pgfmathsetmacro{\ff}{\i*\f}
\draw (0,\i) node {\ff};
\pgfmathsetmacro{\f}{\ff}
}
\end{tikzpicture}
\end{document}

I would have expected this to print the sequence of the factorials of the first ten integers (going down) but what I get is 10,9,...,1. What is happening ? How can I handle such recursively defined functions in Tikz ?

• May be you can use evaluate and count keys from \foreach \i [count=..,evaluate=..] ... – Manuel Jan 8 '15 at 14:50
• Could you expand, I'm not familiar with evaluate and count ? – Thomas Richard Jan 8 '15 at 14:53
• Add \global\let\f=\f at the end of your loop. Another issue: $7!$ is the maximum value computable by TeX (TeX maximum value is 16383.99998). – Paul Gaborit Jan 8 '15 at 15:01
• Well the factorial was just for the example. This seems to have solved my problem. You can make it an answer and I'll accept it. – Thomas Richard Jan 8 '15 at 15:09

The code in the foreach loop is in a TeX group, so all definitions are local to this group. Add \global\let\f=\ff at the end of your loop. As suggested by egreg in his comment, a global macro with a short name is very likely to break something. You may rename \f with a long name.

Another issue: $7!$ is the maximum factorial computable by TeX (TeX maximum value is 16383.99998).

Here is the code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\myglobalfactorial}{1}
\foreach\i in {1,...,7} {
\pgfmathsetmacro{\ff}{\i*\myglobalfactorial}
\draw (0,\i) node {\ff};
%\pgfmathsetmacro{\myglobalfactorial}{\ff}
\global\let\myglobalfactorial=\ff
}
\end{tikzpicture}
\end{document}

Other solution using evaluate and remember keys (the remembered variable is always global so choose a long name):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\foreach \i [evaluate=\i as \ff using \i*\myglobalfactorial,
remember=\ff as \myglobalfactorial (initially 1)] in {1,...,7} {
\draw (0,\i) node {\ff};
}
\end{tikzpicture}
\end{document}
• What is \pgfmathsetmacro{\f}{\ff} supposed to do? Why not simply \global\let\f=\ff? By the way, the first line should be \pgfmathsetmacro{\temp}{<expression>}\global\let\f=\temp. This prevents save stack buildup, that happens when there are global and local assignments to the same variable (in this case the macro \f). Finally, setting globally a macro with a short name is very likely to break something. – egreg Jan 8 '15 at 15:30
• @egreg I keep the lines and the macro names from MWE provided by OP. You are right: it's dangerous and inefficient. – Paul Gaborit Jan 8 '15 at 15:36
• @egreg I edited my answer... – Paul Gaborit Jan 8 '15 at 15:47