Tikz: foreach loop - How to increment index by 1?

From my various comments above.

  1. You can use \numexpr.
  2. You can use evaluate with int.
  3. You can use count=<variable> from <start>.

This list is not exhaustive and apart from \numexpr you can find these things in section 89 Repeating Things: The Foreach Statement of pgfmanual v3.1.5. Notice that one has to be careful when using parse with integers, which is why I did not add it to the list.

These are just some examples and interestingly the last one also works, but without braces.

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \i in {1,...,5}
{\i,\the\numexpr\i+4\relax\par} %<- sometimes one needs a \relax to delimit
% the numexpr stuff from other expressions that could be parsed

\bigskip

\foreach \i [evaluate=\i as \j using {int(\i+4)}] in {1,...,5}
{\i,\j\par} 

\bigskip

\foreach \i [count=\j from 5] in {1,...,5}
{\i,\j\par} 

\bigskip

% if you want to start from an expression, do not add braces 
% i.e. no braces around \i+1
\foreach \i in {1,...,5}
{\foreach \j [count=\k from \i+1] in {1,2,3}
{\i,\j,\k\par}}


\end{document}

tikzmath has some very closely related yet somewhat different options.

Please note that there is also the good old \loop which does not put the stuff it is iterating over in groups (which one can replace by \pgfmathloop in the pgf context). Also pgfplots offers \pgfplotsforeachungrouped, which avoids grouping, too. More information can be found in section 8.1 Utility Commands of the pgfplots (!) manual v1.17. This can be instrumental for defining stuff only locally (which is less relevant for the example in the question, though).

And there are many more loop routines from expl3, listofitems, etoolbox and I am sure I forgot many. Each of them has some particular strengths (and sometimes also weaker points).


I would suggest using the evaluate functionality of the foreach loop:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
   %%% modification according to comments; thanks for pointing it out
   \foreach \i [evaluate=\i as \ieval using {int(\i+42)}] in {1,...,6} {
      \node at (\i,0) {\ieval};
   }
\end{tikzpicture}
\end{document}