# Basic calculations in PGF/TikZ for loop

I've seen a bunch of similar questions and answers relating to the same kind of problem, but they either weren't particularly well explained or something got lost in translation somewhere.

So I thought I'd make this super simple MWE, to clear it up once and for all, for myself and all the current and future LaTeX newbs. That way it should be easy to adapt the technique for more complex use cases as they arise, rather than trying to reverse engineer complicated examples for simple uses.

I hope that makes sense, anyway here it is:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amsmath}
\usepackage{pgffor}
\usepackage{tikz}
\usepackage{calc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usetikzlibrary{calc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\foreach \x in {1,...,5} { $(\x-1)$ }

\end{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


The loop works fine, but the calculation doesn't.
The desired output is simply something along the lines of:
01234
But I keep getting something more literal, like:
(1-1)(2-2)(3-3)(4-4)(5-5)

• Remember that TeX is, before anything else, a typesetting system, so unless you tell it to do otherwise it will write everything on paper. Try $(\the\numexpr\x-1\relax)$. However I'm sure TikZ has a more user friendly way to do that. You can also load the xfp package and use \inteval{\x-1}. – Phelype Oleinik Jun 29 '19 at 13:43

Here are two alternatives to using evaluate. You can use \the\numexpr or just loop over 0,...,4. Only pgffor is needed for that.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgffor}
\begin{document}

\foreach \x in {1,...,5} { $(\the\numexpr\x-1)$ }

\foreach \x in {0,...,4} { $(\x)$ }
\end{document}

• So you just prepend \the\numexpr to the variable \x? Easy enough, but why? Is calc not even required to do the calculation? It seems very strange. Less confusing than the evaluate syntax though, which is nice. I feel like it should just be a delimiter, like how $ is used for typesetting equations. I'm surprised it's not part of TeX proper. – voices Jul 1 '19 at 5:43 Tikz allows to perform calculations on a variable using the syntax [evaluate=\x as... using...] see pages 983 and 984 of the 3.1.3 manual. \documentclass{article} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \usepackage{amsmath} \usepackage{pgffor} \usepackage{tikz} \usepackage{calc} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \usetikzlibrary{calc} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} \foreach \x [evaluate=\x as \xx using int(\x-1)]in {1,...,5} {$\xx$} \end{document}  • So after \foreach \x you have to open square bracket [, tell it to evaluate=\x, give the output a variable as \xx, declare that the variable is an integer using int, slip in the actual calculation using the original variable name inside parentheses (\x-1), close square bracket ] continue writing the loop like normal so it has something to iterate over in {1,...,5}, then finally typeset the evaluated output{$\xx$} – voices Jul 1 '19 at 8:09 • Basically, that's it except that the parentheses around \x-1 are the syntax of the mathematical function int() described on page 1014 in the TikZ manual 3.1.3. – AndréC Jul 1 '19 at 8:17 A proof of concept with expl3: \documentclass{article} \usepackage{xparse,xfp} \ExplSyntaxOn \NewDocumentCommand{\intloop}{O{1}mm} {% #1 = start, default 1; #2 = end; #3 = template \cs_gset_protected:Nn \__tjt_intloop_function:n { #3 } \tjt_intloop:nn { #1 } { #2 } } \cs_new:Nn \tjt_intloop:nn { \int_step_function:nnN { #1 } { #2 } \__tjt_intloop_function:n } \ExplSyntaxOff \begin{document} \intloop{5}{\inteval{#1-1}} \bigskip$\begin{array}{c|c}
n & f(n) \\
\hline
\intloop{5}{#1 & \fpeval{2*(#1)^2-4} \\}
\end{array}\$

\end{document}


• Thanks. I have no idea what I'm looking at though. – voices Jul 1 '19 at 5:45