TikZ-PGF – Using Double For-Loop in TikZ

animationsloopstikz-pgf

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?

Best Answer

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}
Related Question