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.

In the manual one can find the ... option to obtain a range in the for-loop. For example:

\foreach \x in {0,0.1,...,6} {\x, }

On the other hand, one can have a for-loop which runs over two variables simultaneously. For example:

\foreach \i / \y in {1/0,2/0.25,3/0.5,4/0.75,5/1} {
     \only<\i>{
     \draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- cycle;
     \draw (0,0) -- (1,\y);
     }
}

which can produce some simple "animation" when used along with beamer. My question is how to combine the two. Namely, I would like to have something like:

\foreach \i / \y in {1/0,2/0.25,...,5/1}

which will produce the list (1,0),(2,0.25),(3,0.5),(4,0.75),(5,1). Is there a way to do it?

share|improve this question
    
I think it's no (see the pgfmanual). It's only possible with for example : \foreach \x in {0\pi,0.5\pi,...\pi,3\pi} {$\x$, } the ... statement have exactly the same pattern, except that, instead of dots, they have a number or a character. So you need to avoid ... in your case but perhaps I'm wrong and a great expert can find a solution. –  Alain Matthes Jun 25 '11 at 9:42
add comment

1 Answer

up vote 17 down vote accepted

I don't think that this is possible. However, in your case the / notations isn't really necessary:

\begin{tikzpicture}
\foreach \y [count=\i] in {0,0.25,...,1} {
     \only<\i>{
     \draw (0,0) rectangle (1,1);
     \draw (0,0) -- (1,\y);
     }
}
\end{tikzpicture}

For more complicated cases, when one variable is dependent on the other via some formula, you can also use

\begin{tikzpicture}
\foreach \i [evaluate=\i as \y using (\i-1)*0.25] in {1,2,...,5} {
     \only<\i>{
     \draw (0,0) rectangle (1,1);
     \draw (0,0) -- (1,\y);
     }
}
\end{tikzpicture}
share|improve this answer
    
Your first snippet solved my problem! Can you give a reference to these two solutions? So I can understand them better? Thnx anyway! –  Dror Jun 25 '11 at 10:29
1  
@Dror: pages 507 and 508 of the TikZ v2.10 manual (“Options to customize the foreach-statement”). –  Caramdir Jun 25 '11 at 10:31
    
See also tex.stackexchange.com/questions/21557/… –  Caramdir Jun 25 '11 at 10:31
    
Thnx!! I stopped on page 505... –  Dror Jun 25 '11 at 13:37
add comment

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.