# Increments in \foreach loop with two variables, TikZ

I'm trying to draw a stack of arrows at different angles in a \foreach loop in TikZ. I don't want to have to declare every z coordinate and angle manually so I was using the {1,2,...,10} syntax but this doesn't seem to work in the example below with two variables in the for loop.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{arrows}
\usetikzlibrary{3d}
\tikzset{>=latex}

\begin{document}

\tdplotsetmaincoords{90}{90}
\tdplotsetrotatedcoords{0}{20}{70}

\begin{tikzpicture}[tdplot_rotated_coords,scale=0.5]

\foreach \x/\y in {7/0, 8/10, ..., 10/30}
{
\draw (2, 2, \x) circle(2) node[right]{\y};
\draw[->, ultra thick, red] (2, 2, \x) --++ (\y:2) --++ (\y+180:4);
}

\end{tikzpicture}
\end{document}


Does anyone know how to get around this? The code above gives the error ! Illegal unit of measure (pt inserted).

Thanks.

• tikz seems unable to "fill in the gaps" associated with .... Apr 14 '14 at 15:15
• Is the relationship between \x and \y expressable using a formula? Apr 14 '14 at 15:20
• For this specific case, looping over \x only and computing \y with \pgfmathtruncatemacro{\y}{10*(\x-7)} seems to work. Can this be applied to your original problem as well? Apr 14 '14 at 15:20
• Unfortunately there isn't a general relationship between \x and \y, I'd like to do it this way so that I can change \y a lot while keeping \x the same. Apr 14 '14 at 15:24
• Apr 14 '14 at 15:39

The problem stems from the ... part in the argument of the \foreach macro; note that it disappears if you delete ..., from your code.

Although you can of course recognise a pattern in

7/0, 8/10, ..., 10/30


the \foreach macro cannot. I refer you to section 56 of the tikz manual and to using computations with \foreach in tikz for more details about how ... works inside the argument of \foreach.

In this particular case, under the assumption that \x represent a sequence that can be expressed by a simple enough formula—\x simply represents an arithmetic sequence, here—you can just use \y as the only loop variable and derive \x from the iteration variable; I've defined the latter as \i by using count=\i in the optional argument of \foreach, below.

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{arrows}
\usetikzlibrary{3d}
\tikzset{>=latex}

\begin{document}

\tdplotsetmaincoords{90}{90}
\tdplotsetrotatedcoords{0}{20}{70}

\begin{tikzpicture}[tdplot_rotated_coords,scale=0.5]

\foreach[count=\i, evaluate=\i as \x using int(\i+6)] \y in {0,10,...,30}
{
\draw (2, 2, \x) circle(2) node[right] {\y};
\draw[->, ultra thick, red] (2, 2, \x) --++ (\y:2) --++ (\y+180:4);
}

\end{tikzpicture}
\end{document}

• Try '\foreach[count=\x from 7] \y in {0,10,...,30}' and no pgfmathtrunc... Apr 14 '14 at 16:48
• @Tarass You're right; I could do that in this specific case. However, my approach is more general, because it can be adapted to cases where the recursive formula for the \x sequence is different from simply x_{n+1} = x_n + 1. Apr 14 '14 at 17:00
• Then on can do foreach \i [evaluate=\i as \x using \i+7, evaluate=\i as \y using 30*\i] in {0,...,3} or what other formula you want. Apr 14 '14 at 17:58
• @Tarass But then, how would you specify \y? The OP wrote that there is no simple relation between \x and \y. I'm assuming that \x and \y cannot simply be deduced from a common variable... Apr 14 '14 at 18:11
• @Jubobs: why not adding the evaluate option? Apr 15 '14 at 12:33