Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
tikz seems unable to "fill in the gaps" associated with .... –  Jubobs Apr 14 at 15:15
    
Is the relationship between \x and \y expressable using a formula? –  Gonzalo Medina Apr 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? –  T. Verron Apr 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. –  user3087409 Apr 14 at 15:24
    
related: tex.stackexchange.com/questions/142188/… –  Jubobs Apr 14 at 15:39

1 Answer 1

up vote 10 down vote accepted

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}
share|improve this answer
    
Try '\foreach[count=\x from 7] \y in {0,10,...,30}' and no pgfmathtrunc... –  Tarass Apr 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. –  Jubobs Apr 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 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... –  Jubobs Apr 14 at 18:11
1  
@Jubobs: why not adding the evaluate option? –  Claudio Fiandrino Apr 15 at 12:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.