I am using a \foreach
loop in TikZ with \i in {2,3,..,5}
and I'd like to be able to use the value \i-1
. However, every approach I have tried gives me a Illegal unit of measure (pt inserted)
error. Below are three minimal examples with my different attempts after reading some answer in this and other forums:
With the evaluate
option:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (N1) {N1};
\foreach \i [evaluate=\i as \j using \i-1] in {2,3,..,5}{
% whatever
}
\end{tikzpicture}
\end{document}
With \pgfmathtruncatemacro
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (N1) {N1};
\foreach \i in {2,3,..,5}{
\pgfmathtruncatemacro{\j}{\i - 1}
% whatever
}
\end{tikzpicture}
\end{document}
With \pgfmathparse
:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (N1) {N1};
\foreach \i in {2,3,..,5}{
\pgfmathparse{\i - 1}
% whatever
}
\end{tikzpicture}
\end{document}
I have seen plenty of similar examples supposed to work, what am I missing?
My version of PGF/TikZ (as reported by \pgfversion
) is 3.0.0.
PD: I know that foreach
supports iterating over two variables at the same time, so I could use \foreach \i / \j in {2/1, 3/2, 4/3, 5/4}
, but that doesn't scale too well.
Best Answer
You need to use three periods in the
2,3,...,5
expression, not two.