Open main menu

Changes

Added \dorecurse and friends
< '''Prev:''' [[System Macros/Branches and Decisions|Brances & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] >



TeX does not offer us powerfull 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.


=== <cmd>dorecurse</cmd> ===
<texcode>
\dorecurse {n} {whatever we want}
</texcode>

This macro can be nested without problems and therefore be
used in situations where Plain TeX's <code>\loop</code> macro
ungracefully fails. The current value of the counter is
available in <code>\recurselevel</code>, before as well as after
the <code>whatever we want</code> stuff.

<context source="yes">
\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}
</context>

In this example the first, second and fourth
<code>\recurselevel</code> concern the outer loop, while the third
and fifth one concern the inner loop. The depth of the
nesting is available for inspection in <code>\recursedepth</code>.

Both <code>\recurselevel</code> and <code>\recursedepth</code> are
macros. The real conters are hidden from the user because
we don't want any interference.


=== <cmd>dostepwiserecurse</cmd> ===
The simple command <code>\dorecurse</code> is
a special case of the more general:

<texcode>
\dostepwiserecurse {from} {to} {step} {action}
</texcode>

This commands accepts positive and negative steps. Illegal
values are handled as good as possible and the macro accepts
numbers and counters.

<texcode>
\dostepwiserecurse {1} {10} {2} {...}
\dostepwiserecurse {10} {1} {-2} {...}
</texcode>

=== <cmd>doloop</cmd> <cmd>exitloop</cmd> ===
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
explictly exit it. Nesting is supported. First we present
a more extensive alternative.

<texcode>
\doloop
{Some kind of typesetting punishment \par
\ifnum\pageno>100 \exitloop \fi}
</texcode>

When needed, one can call for <code>\looplevel</code> and
<code>\loopdepth</code>.

The loop is executed at least once, so beware of situations
like:

<texcode>
\doloop {\exitloop some commands}
</texcode>

It's just a matter of putting the text into the <code>\if</code>
statement that should be there anyway, like in:

<texcode>
\doloop {\ifwhatever \exitloop \else some commands\fi}
</texcode>

You can also quit a loop immediately, by using <code>
\exitloopnow</code> instead. Beware, this is more sensitive
for conditional errors.

[[Category:ConTeXt programming]]
[[Category:Inside ConTeXt]]