[Tex/LaTex] Increments in \foreach loop with two variables, TikZ

foreachtikz-pgf

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.

Best Answer

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.

enter image description here

\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}