27

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.

thosphor
  • 1,193
  • 1
  • 12
  • 24

1 Answers1

25

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}
jub0bs
  • 58,916
  • 2
    Try '\foreach[count=\x from 7] \y in {0,10,...,30}' and no pgfmathtrunc... – Tarass Apr 14 '14 at 16:48
  • 2
    @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. – jub0bs 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. – Tarass Apr 14 '14 at 17:58
  • 1
    @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... – jub0bs Apr 14 '14 at 18:11
  • If on use ... in the for each, it means that the two \x and \y have an arithmetic incrementation. Then what I have wrote works. Otherwise you have to write explicitely the parameter (even both parameters). If one is inarithmetic progression one can use your solution. But in the OP's example both are in arithmetic progression. My solution works far all type of sequences even not artiyhmetics. If \x nor \y are not a sequence, one have to givve all values explicitely. – Tarass Apr 14 '14 at 18:58
  • 2
    @Jubobs: why not adding the evaluate option? – Claudio Fiandrino Apr 15 '14 at 12:33
  • @ClaudioFiandrino You're right. I hadn't realised until now that you could use evaluate on the count variable! Thanks for the tip. I'll edit my answer. – jub0bs Apr 15 '14 at 12:39
  • @Jubobs: you're welcome :) – Claudio Fiandrino Apr 15 '14 at 12:45