12

Is it possible to have while loops in TikZ, or some equivalent loop which only terminates after a certain condition?

1
  • It's possible to use pgffor package outside of TikZ. The loops are not a real part of TikZ also you can use what you want to make loop, while loop etc. Dec 28, 2011 at 13:13

2 Answers 2

15

You can combine the loop \foreach with the provided command \breakforeach. The following example is copied from the manual.

\begin{tikzpicture}
\foreach \x in {1,...,4}
 \foreach \y in {1,...,4}
   {
    \fill[red!50] (\x,\y) ellipse (3pt and 6pt);
    \ifnum \x<\y
     \breakforeach
    \fi
   }
\end{tikzpicture}
1
  • However, the current execution of the <commands> is continued normally, so it is probably best to use this command only at the end of a \foreach command.
    – dEmigOd
    Mar 22, 2020 at 12:13
8

Update \whiledo is defined in the package ifthen and not calc

TikZ is based on TeX also you can build a while loop with TeX but it's also possible to use different packages like ifthen. With ifthen you can use the macro \whiledo and macros from TikZ.

1
  • 1
    You are right, this a mistake. I would like to say ifthen. Sorry Dec 28, 2011 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .