Forest
Reference
3.10 Loops¶
All loops take a ⟨keylist⟩ argument. The interpretation of the keys in these keylists depends on the environment the loop appears in. If it is a part of a nodewalk specification, the keys are taken to be nodewalk keys (§3.8), otherwise node keys (§3.5).
All loops can be nested safely.
repeat=⟨number⟩⟨keylist⟩(loop)¶
The ⟨keylist⟩ is processed ⟨number⟩ times.
The ⟨number⟩ expression is evaluated using pgfmath.
while=⟨forestmath: condition⟩⟨keylist⟩(loop)¶
do while=⟨forestmath: condition⟩⟨keylist⟩(loop)¶
until=⟨forestmath: condition⟩⟨keylist⟩(loop)¶
do until=⟨forestmath: condition⟩⟨keylist⟩(loop)¶
If a loop “walks out of the tree”, you can get it back in using last valid or strip fake steps.
while loops cycle while the condition is true, until loops terminate when the condition becomes true.
The do variants check the condition after processing the ⟨keylist⟩; thus, the keylist is executed at least once. The variants without the do prefix check the condition before processing the ⟨keylist⟩, which might therefore not be processed at all.
When ⟨forestmath: condition⟩ is an empty string, condition valid is implicitely used, and ⟨keylist⟩ is implicitely embedded in on invalid=fake. Thus, the while loops will cycle until they “walk out of the tree”, and until loops will cycle until they “walk into the tree.”
\pgfmathsetseed{1234}
\begin{forest}
try/.style={root',content+={#1,\ },n=#1},
delay={
for nodewalk={~do until~={}{try/.pgfmath={random(1,20)}},draw}{},
},
[tried:\ [1][2][3][4][5]]
\end{forest}
while nodewalk valid=⟨nodewalk⟩⟨keylist⟩(loop)¶
do while nodewalk valid=⟨nodewalk⟩⟨keylist⟩(loop)¶
until nodewalk valid=⟨nodewalk⟩⟨keylist⟩(loop)¶
do until nodewalk valid=⟨nodewalk⟩⟨keylist⟩(loop)¶
while nodewalk empty=⟨nodewalk⟩⟨keylist⟩(loop)¶
do while nodewalk empty=⟨nodewalk⟩⟨keylist⟩(loop)¶
until nodewalk empty=⟨nodewalk⟩⟨keylist⟩(loop)¶
do until nodewalk empty=⟨nodewalk⟩⟨keylist⟩(loop)¶
⟨nodewalk⟩ is embedded within on invalid=fake.
break=\(n\)(default 0)¶
Break out of the loop.
The loop is only exited after all the keys in the current cycle have been processed.
The optional argument \(n\) (\(n\geq0\)) specifies which level of embedding to break from; the default is to break out of the innermost loop.
forestloopcount([\(n\)])(pgfmath function, default 0)¶
How many times has the loop repeated until now?
The optional argument \(n\) (\(n\geq0\)) specifies the level of embedding to provide information for; the default is to count the repetitions of the current, most deeply embedded loop.