# using '\foreach' loops to draw tikz figures

I am using \usepackage{tikz} to draw something using xyz coordinates. I have a bunch of coordinates named (xyt) such as (000) (001) (020)...(001)(011) etc, they are created by:

\coordinate (ijk) at (i,k,j);


e.g.

\coordinate (001) at (0,1,0);


I want to write a loop to draw something like:

\draw (110)--(000)--(011)--(020);
\draw (111)--(001)--(012)--(021);
\draw (112)--(002)--(013)--(022);
etc.


Essentially what I want to do is (in python language):

for i in range(9):
j=i+1
\draw (11i) -- (00i) -- (01j) -- (02i);


And I tried:

\foreach \time in {0,1,2,3,4,5,6,7,8,9}{
\draw (11\time) -- (00\time) -- (01{\time+1}) -- (02\time);
}


However, Tikz identify {\time+1} as {1+1},{2+1},... instead of 2,3,..., while (00\time) is causing no problem.

Any help is appreciated, Thanks!

• foreach \time [evaluate=\time as \suivant using int(\time+1)]{\draw (11\time) -- (00\time) -- (01{\suivant}) -- (02\time);} – AndréC Jul 14 '19 at 5:51
• @AndréC Nice solution. If you write up this comment as a formal answer, I will accept that. – Sylvester W. Zhang Jul 14 '19 at 18:51
• Okay, I'll do it. – AndréC Jul 14 '19 at 18:56
• I just published a response. Is that what you wanted to do? If not exactly that, please specify your question. – AndréC Jul 14 '19 at 21:11

The syntax of foreach loops increments by 1 implicitly, so it is not necessary to list all integers from 0 to 9, just write {0,...,9}.

\foreach \x in {0,...,9}{  };


In the loop where you calculate the next integer \time+1, since the coordinates go from (000) to (999) it is mandatory to limit the list which must go from 0 to 8. Because otherwise we obtain 9+1=10, which produces an non-existent coordinate (0110).

\foreach \time [evaluate=\time as \suivant using int(\time+1)]in {0,...,8}
\draw (11\time) -- (00\time) -- (01\suivant) -- (02\time);


I don't know if that's the result you want to achieve. I tried to understand by sticking back the little pieces of code from your question. I hope that this is convenient for you, otherwise please specify your question.

\documentclass[tikz,border=5mm]{standalone}

\begin{document}

\begin{tikzpicture}[]
\foreach \x in {0,...,9}
\foreach \y in {0,...,9}
\foreach \z in {0,...,9}
\coordinate (\x\y\z) at (\x,\y,\z);

\foreach \time [evaluate=\time as \suivant using int(\time+1)]in {0,...,8}
\draw (11\time) -- (00\time) -- (01\suivant) -- (02\time);
\end{tikzpicture}

\end{document}


Translated with www.DeepL.com/Translator

Okay, found an easy fix;

It turns out that I just need to use

\foreach \time in {0,1,2,3,4,5,6,7,8,9}{
\draw (1,\time,1) -- (0,\time,0) -- (0,\time+1,1) -- (0,\time,2);
}