Difference between revisions of "System Macros/Loops and Recursion"
m (powerfull-->powerful) |
|||
Line 1: | Line 1: | ||
< '''Prev:''' [[System Macros/Branches and Decisions|Brances & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] > | < '''Prev:''' [[System Macros/Branches and Decisions|Brances & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] > | ||
− | TeX does not offer us | + | TeX does not offer us powerful for-loop mechanisms. On |
the other hand its recursion engine is quite unique. We | the other hand its recursion engine is quite unique. We | ||
therefore identify the for-looping macros by this method. | therefore identify the for-looping macros by this method. | ||
Line 96: | Line 96: | ||
[[Category:ConTeXt programming]] | [[Category:ConTeXt programming]] | ||
[[Category:Inside ConTeXt]] | [[Category:Inside ConTeXt]] | ||
+ | /code> and |
Revision as of 09:10, 14 June 2012
< Prev: Brances & Decisions | Top: System Macros | Next: Action Processing >
TeX does not offer us powerful for-loop mechanisms. On the other hand its recursion engine is quite unique. We therefore identify the for-looping macros by this method. The most simple alternative is the one that only needs a number.
\dorecurse
\dorecurse {n} {whatever we want}
This macro can be nested without problems and therefore be
used in situations where Plain TeX's \loop
macro
ungracefully fails. The current value of the counter is
available in \recurselevel
, before as well as after
the whatever we want
stuff.
\dorecurse % inner loop {10} {\recurselevel: % outer value \dorecurse % inner loop {\recurselevel} % outer value {\recurselevel} % inner value \dorecurse % inner loop {\recurselevel} % outer value {\recurselevel} % inner value \endgraf}
In this example the first, second and fourth
\recurselevel
concern the outer loop, while the third
and fifth one concern the inner loop. The depth of the
nesting is available for inspection in \recursedepth
.
Both \recurselevel
and \recursedepth
are
macros. The real conters are hidden from the user because
we don't want any interference.
\dostepwiserecurse
The simple command \dorecurse
is
a special case of the more general:
\dostepwiserecurse {from} {to} {step} {action}
This commands accepts positive and negative steps. Illegal values are handled as good as possible and the macro accepts numbers and counters.
\dostepwiserecurse {1} {10} {2} {...} \dostepwiserecurse {10} {1} {-2} {...}
\doloop \exitloop
Sometimes loops are not determined by counters, but by (a combinations of) conditions. We therefore implement a straightforward loop, which can only be left when we explicitly exit it. Nesting is supported. First we present a more extensive alternative.
\doloop {Some kind of typesetting punishment \par \ifnum\pageno>100 \exitloop \fi}
When needed, one can call for \looplevel
and
\loopdepth
.
The loop is executed at least once, so beware of situations like:
\doloop {\exitloop some commands}
It's just a matter of putting the text into the \if
statement that should be there anyway, like in:
\doloop {\ifwhatever \exitloop \else some commands\fi}
You can also quit a loop immediately, by using
\exitloopnow
instead. Beware, this is more sensitive
for conditional errors.
< Prev: Brances & Decisions | Top: System Macros | Next: Action Processing > /code> and