https://wiki.contextgarden.net/api.php?action=feedcontributions&user=Hajtmar&feedformat=atomWiki - User contributions [en]2024-03-29T08:55:04ZUser contributionsMediaWiki 1.34.1https://wiki.contextgarden.net/index.php?title=System_Macros/Loops_and_Recursion&diff=22975System Macros/Loops and Recursion2015-09-29T08:48:49Z<p>Hajtmar: </p>
<hr />
<div>< '''Prev:''' [[System Macros/Branches and Decisions|Branches & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] ><br />
<br />
TeX does not offer us powerful for-loop mechanisms. On<br />
the other hand its recursion engine is quite unique. We<br />
therefore identify the for-looping macros by this method.<br />
The most simple alternative is the one that only needs a<br />
number.<br />
<br />
=== <cmd>dorecurse</cmd> ===<br />
<texcode><br />
\dorecurse {n} {whatever we want}<br />
</texcode><br />
<br />
This macro can be nested without problems and therefore be<br />
used in situations where Plain TeX's <code>\loop</code> macro<br />
ungracefully fails. The current value of the counter is<br />
available in <code>\recurselevel</code>, before as well as after<br />
the <code>whatever we want</code> stuff.<br />
<br />
<context source="yes"><br />
\dorecurse % inner loop<br />
{10}<br />
{\recurselevel: % outer value<br />
\dorecurse % inner loop<br />
{\recurselevel} % outer value<br />
{\recurselevel} % inner value<br />
\dorecurse % inner loop<br />
{\recurselevel} % outer value<br />
{\recurselevel} % inner value<br />
\endgraf}<br />
</context><br />
<br />
In this example the first, second and fourth<br />
<code>\recurselevel</code> concern the outer loop, while the third<br />
and fifth one concern the inner loop. The depth of the<br />
nesting is available for inspection in <code>\recursedepth</code>.<br />
<br />
Both <code>\recurselevel</code> and <code>\recursedepth</code> are<br />
macros. The real conters are hidden from the user because<br />
we don't want any interference.<br />
<br />
<br />
=== <cmd>dostepwiserecurse</cmd> ===<br />
The simple command <code>\dorecurse</code> is<br />
a special case of the more general:<br />
<br />
<texcode><br />
\dostepwiserecurse {from} {to} {step} {action}<br />
</texcode><br />
<br />
This commands accepts positive and negative steps. Illegal<br />
values are handled as good as possible and the macro accepts<br />
numbers and counters.<br />
<br />
<texcode><br />
\dostepwiserecurse {1} {10} {2} {...}<br />
\dostepwiserecurse {10} {1} {-2} {...}<br />
</texcode><br />
<br />
=== <cmd>doloop</cmd> <cmd>exitloop</cmd> ===<br />
Sometimes loops are not determined by counters, but by<br />
(a combinations of) conditions. We therefore implement a<br />
straightforward loop, which can only be left when we<br />
explicitly exit it. Nesting is supported. First we present<br />
a more extensive alternative.<br />
<br />
<texcode><br />
\doloop<br />
{Some kind of typesetting punishment \par<br />
\ifnum\pageno>100 \exitloop \fi}<br />
</texcode><br />
<br />
When needed, one can call for <code>\recurselevel</code> and<br />
<code>\recursedepth</code>.<br />
<br />
The loop is executed at least once, so beware of situations<br />
like:<br />
<br />
<texcode><br />
\doloop {\exitloop some commands}<br />
</texcode><br />
<br />
It's just a matter of putting the text into the <code>\if</code><br />
statement that should be there anyway, like in:<br />
<br />
<texcode><br />
\doloop {\ifwhatever \exitloop \else some commands\fi}<br />
</texcode><br />
<br />
You can also quit a loop immediately, by using <code><br />
\exitloopnow</code> instead. Beware, this is more sensitive<br />
for conditional errors.<br />
<br />
=== Recursion and expansion ===<br />
<br />
Using recursion to build tables requires some particular attention:<br />
<br />
<context source="yes"><br />
\bTABLE<br />
\bTR<br />
\dorecurse{8}{\expanded{\bTD\recurselevel\eTD}}<br />
\eTR<br />
\eTABLE<br />
</context><br />
<br />
Multi-dimensional tables offer another illustration:<br />
<br />
<context source="yes"><br />
\bTABLE<br />
\dorecurse{8}{<br />
\bTR<br />
\dorecurse{5}{\bTD #1,##1 \eTD}<br />
\eTR<br />
}<br />
\eTABLE<br />
</context><br />
<br />
Alternatively, (mkiv only?)<br />
<br />
<context source="yes"><br />
\bTABLE<br />
\dorecurse{8}{\bTR<br />
\dorecurse{5}{\bTD \currentTABLErow,\currentTABLEcolumn \eTD}<br />
\eTR}<br />
\eTABLE<br />
</context><br />
<br />
For further discussion on loops and expansion, see [[System_Macros/Expansion_Control|Expansion control]] as well as this [http://randomdeterminism.wordpress.com/2009/03/05/tex-programming-the-past-the-present-and-the-future/ blog post].<br />
<br />
< '''Prev:''' [[System Macros/Branches and Decisions|Branches & Decisions]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/Action Processing|Action Processing]] ><br />
<br />
[[Category:ConTeXt programming]]<br />
[[Category:Inside ConTeXt]]</div>Hajtmarhttps://wiki.contextgarden.net/index.php?title=Hiding_Content&diff=21794Hiding Content2013-05-29T06:53:36Z<p>Hajtmar: </p>
<hr />
<div>< [[Visuals]] | [[Presentations]] | [[Layers]] ><br />
<br />
Probably for your [[Presentations|slides]], probably for some [[Animation|animation]] or for whatever reason you may want it: you can hide some content from the document, while leaving exactly the same amount of space as that content would need if typeset in the usual way.<br />
<br />
== Overview ==<br />
<br />
On the [[http://archive.contextgarden.net/thread/20050707.223338.a9169811.html mailing list]] Hans suggested many different ways (astonishing easy to use) to hide the content in a PDF document. Sadly, most of them are viewer-dependent.<br />
<br />
<table style="border:1px solid #DDDDDD; width:100%" valign="top"><br />
<br />
<tr style="background-color:#DDDDDD" align="left" valign="top"><th> method </th><th> principle </th><th> PROS </th><th> CONS </th></tr><br />
<br />
<tr><td><br />
<texcode><br />
\defineproperty[my-secrets][layer][state=top]<br />
\startproperty[my-secrets]<br />
My public secret.<br />
\stopproperty<br />
</texcode><br />
</td><td><br />
* makes (invisible) layer on top (JS-based)<br />
</td><td><br />
* completely hides everything<br />
</td><td><br />
* works with newer Adobe Acrobat/Viewer, in most other browsers the content is not hidden<br />
* I managed to freeze Acrobat 6.0 pro when trying to save the text<br />
</td></tr><br />
<br />
<tr align="left" valign="top" style="background-color:#EEEEEE;"><br />
<td><br />
<texcode><br />
\defineproperty[my-hasitations][layer][state=stop]<br />
<br />
\startproperty[my-hasitations]<br />
To \ConTeXt\ or not to \ConTeXt?<br />
\stopproperty<br />
<br />
\button{Show Decision}[VideLayer{my-hasitations}]<br />
\button{Hide Decision}[HideLayer{my-hasitations}]<br />
\button{Toggle Decision}[ToggleLayer{my-hasitations}]<br />
</texcode><br />
</td><br />
<td><br />
* makes (invisible) layer, which can be switched on and off (JS-based)</td><br />
<td><br />
* you can switch the content on and off<br />
* when hidden, it is completely hidden (as above)<br />
</td><td><br />
* (same as above)<br />
* even some new Acrobat versions can't handle the buttons<br />
</td></tr><br />
<br />
<tr><td><br />
<texcode><br />
\starteffect[hidden] % In MkIV<br />
My secret hidden for Adam's apple ;)<br />
\stopeffect<br />
</texcode><br />
</td><td><br />
* makes transparent fonts<br />
</td><td><br />
* no JavaScript<br />
* pretty safe for different viewers<br />
</td><td><br />
* only text is hidden, but not images, rules, ...<br />
</td></tr><br />
<br />
<tr align="left" valign="top" style="background-color:#EEEEEE;"><br />
<td><br />
[[Hiding Content#The plain TeX way|the plain TeX way]]<br />
</td><td><br />
* puts a blank box instead of its content (letter)<br />
</td><td><br />
* <b>absolutely no</b> content in the document, not even hidden (only blank space)<br />
</td><td><br />
* only works for letters and whitespace, no macros, no figures, ...<br />
</td></tr><br />
</table><br />
<br />
== The plain TeX way ==<br />
<br />
This example was adapted from the TeXBOOK. However, it can only handle usual text. Forget about macros, forget about graphics ...<br />
<br />
I hope and still believe that it is possible to extend this macro to make it work in general case. This would also be a preferred solution as it doesn't depend on the viewer's JavaScript <i>in</i>capabilities.<br />
<br />
<texcode><br />
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br />
% the portion of code adapted from the TeXBOOK, Excercise 11.5 %<br />
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br />
<br />
\def\dolist{\afterassignment\dodolist\let\next= }<br />
\def\dodolist{\ifx\next\endlist\let\next\relax<br />
\else \\\let\next\dolist \fi<br />
\next}<br />
\def\endlist{\endlist}<br />
<br />
\def\\{\if\space\next\ % assume that \next is unexpandable<br />
\else\setbox0=\hbox{\next}\setbox1=\hbox{}%<br />
\dp1=\dp0\ht1=\ht0\wd1=\wd0\box1\fi}<br />
<br />
\def\demobox#1{{\dolist#1\endlist}}<br />
<br />
</texcode><br />
<br />
<context><br />
<br />
\def\dolist{\afterassignment\dodolist\let\next= }<br />
\def\dodolist{\ifx\next\endlist\let\next\relax<br />
\else \\\let\next\dolist \fi<br />
\next}<br />
\def\endlist{\endlist}<br />
<br />
\def\\{\if\space\next\ % assume that \next is unexpandable<br />
\else\setbox0=\hbox{\next}\setbox1=\hbox{}%<br />
\dp1=\dp0\ht1=\ht0\wd1=\wd0\box1\fi}<br />
<br />
\def\demobox#1{{\dolist#1\endlist}}<br />
<br />
Please write something more interesting on this place<br />
\demobox{if you are visiting this page and getting bored.}<br />
Thanks.<br />
<br />
Please write something more interesting on this place<br />
if you are visiting this page and getting bored.<br />
Thanks.<br />
<br />
</context><br />
<br />
== Layer: state=top ==<br />
<br />
You can try something like:<br />
<br />
<texcode><br />
\defineproperty[my-secrets][layer][state=top]<br />
<br />
I don't tell this to everybody:<br />
%<br />
\startproperty[my-secrets]%<br />
\quotation{\ConTeXt\ goes beyond the limits of imagination, that's why I love it!}<br />
\stopproperty<br />
%<br />
You'll understand that once you get to know it better.<br />
</texcode><br />
<br />
which results in<br />
<br />
<context><br />
\setupcolors[state=start]<br />
\defineproperty[my-secrets][layer][state=top]<br />
<br />
I don't tell this to everybody:<br />
%<br />
\startproperty[my-secrets]%<br />
%<br />
% for the curious eyes: this effect was faked on Wiki as it doesn't work otherwise<br />
% (when PNG is generated from the PDF, the hidden layer becomes visible)<br />
%<br />
\color[white]{\quotation{\ConTeXt\ goes beyond the limits of imagination, that's why I love it!}}<br />
\stopproperty<br />
%<br />
You'll understand that once you get to know it better.<br />
</context><br />
<br />
This can be compared to the non-hidden version, which is:<br />
<br />
<context><br />
I don't tell this to everybody:<br />
{\quotation{\ConTeXt\ goes beyond the limits of imagination, that's why I love it!}}<br />
You'll understand that once you get to know it better.<br />
</context><br />
<br />
== Layer: state=stop ==<br />
<br />
== Transparent fonts ==<br />
<br />
== Final Remarks ==<br />
<br />
* <b>Just as a hint</b>: Don't ever think about hiding your secrets or solutions to the problems for your students that way!<br><br />
If you happen to have Adobe Acrobat Professional, you can select <i>Layers</i> on the left and switch the hidden layer on again.<br><br />
<font color="red">YOU HAVE BEEN WARNED!</font><br />
<br />
* kerning: could cause micro differences in the exact placemet of the (hidden) content<br />
<br />
[[Category:Presentation]]</div>Hajtmarhttps://wiki.contextgarden.net/index.php?title=Hiding_Content&diff=21793Hiding Content2013-05-29T06:52:17Z<p>Hajtmar: Name of the command has been changed (\startproperty[hidden] to \starteffect[hidden]).</p>
<hr />
<div>< [[Visuals]] | [[Presentations]] | [[Layers]] ><br />
<br />
Probably for your [[Presentations|slides]], probably for some [[Animation|animation]] or for whatever reason you may want it: you can hide some content from the document, while leaving exactly the same amount of space as that content would need if typeset in the usual way.<br />
<br />
== Overview ==<br />
<br />
On the [[http://archive.contextgarden.net/thread/20050707.223338.a9169811.html mailing list]] Hans suggested many different ways (astonishing easy to use) to hide the content in a PDF document. Sadly, most of them are viewer-dependent.<br />
<br />
<table style="border:1px solid #DDDDDD; width:100%" valign="top"><br />
<br />
<tr style="background-color:#DDDDDD" align="left" valign="top"><th> method </th><th> principle </th><th> PROS </th><th> CONS </th></tr><br />
<br />
<tr><td><br />
<texcode><br />
\defineproperty[my-secrets][layer][state=top]<br />
\startproperty[my-secrets]<br />
My public secret.<br />
\stopproperty<br />
</texcode><br />
</td><td><br />
* makes (invisible) layer on top (JS-based)<br />
</td><td><br />
* completely hides everything<br />
</td><td><br />
* works with newer Adobe Acrobat/Viewer, in most other browsers the content is not hidden<br />
* I managed to freeze Acrobat 6.0 pro when trying to save the text<br />
</td></tr><br />
<br />
<tr align="left" valign="top" style="background-color:#EEEEEE;"><br />
<td><br />
<texcode><br />
\defineproperty[my-hasitations][layer][state=stop]<br />
<br />
\startproperty[my-hasitations]<br />
To \ConTeXt\ or not to \ConTeXt?<br />
\stopproperty<br />
<br />
\button{Show Decision}[VideLayer{my-hasitations}]<br />
\button{Hide Decision}[HideLayer{my-hasitations}]<br />
\button{Toggle Decision}[ToggleLayer{my-hasitations}]<br />
</texcode><br />
</td><br />
<td><br />
* makes (invisible) layer, which can be switched on and off (JS-based)</td><br />
<td><br />
* you can switch the content on and off<br />
* when hidden, it is completely hidden (as above)<br />
</td><td><br />
* (same as above)<br />
* even some new Acrobat versions can't handle the buttons<br />
</td></tr><br />
<br />
<tr><td><br />
<texcode><br />
\starteffect[hidden]<br />
My secret hidden for Adam's apple ;)<br />
\stopeffect<br />
</texcode><br />
</td><td><br />
* makes transparent fonts<br />
</td><td><br />
* no JavaScript<br />
* pretty safe for different viewers<br />
</td><td><br />
* only text is hidden, but not images, rules, ...<br />
</td></tr><br />
<br />
<tr align="left" valign="top" style="background-color:#EEEEEE;"><br />
<td><br />
[[Hiding Content#The plain TeX way|the plain TeX way]]<br />
</td><td><br />
* puts a blank box instead of its content (letter)<br />
</td><td><br />
* <b>absolutely no</b> content in the document, not even hidden (only blank space)<br />
</td><td><br />
* only works for letters and whitespace, no macros, no figures, ...<br />
</td></tr><br />
</table><br />
<br />
== The plain TeX way ==<br />
<br />
This example was adapted from the TeXBOOK. However, it can only handle usual text. Forget about macros, forget about graphics ...<br />
<br />
I hope and still believe that it is possible to extend this macro to make it work in general case. This would also be a preferred solution as it doesn't depend on the viewer's JavaScript <i>in</i>capabilities.<br />
<br />
<texcode><br />
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br />
% the portion of code adapted from the TeXBOOK, Excercise 11.5 %<br />
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br />
<br />
\def\dolist{\afterassignment\dodolist\let\next= }<br />
\def\dodolist{\ifx\next\endlist\let\next\relax<br />
\else \\\let\next\dolist \fi<br />
\next}<br />
\def\endlist{\endlist}<br />
<br />
\def\\{\if\space\next\ % assume that \next is unexpandable<br />
\else\setbox0=\hbox{\next}\setbox1=\hbox{}%<br />
\dp1=\dp0\ht1=\ht0\wd1=\wd0\box1\fi}<br />
<br />
\def\demobox#1{{\dolist#1\endlist}}<br />
<br />
</texcode><br />
<br />
<context><br />
<br />
\def\dolist{\afterassignment\dodolist\let\next= }<br />
\def\dodolist{\ifx\next\endlist\let\next\relax<br />
\else \\\let\next\dolist \fi<br />
\next}<br />
\def\endlist{\endlist}<br />
<br />
\def\\{\if\space\next\ % assume that \next is unexpandable<br />
\else\setbox0=\hbox{\next}\setbox1=\hbox{}%<br />
\dp1=\dp0\ht1=\ht0\wd1=\wd0\box1\fi}<br />
<br />
\def\demobox#1{{\dolist#1\endlist}}<br />
<br />
Please write something more interesting on this place<br />
\demobox{if you are visiting this page and getting bored.}<br />
Thanks.<br />
<br />
Please write something more interesting on this place<br />
if you are visiting this page and getting bored.<br />
Thanks.<br />
<br />
</context><br />
<br />
== Layer: state=top ==<br />
<br />
You can try something like:<br />
<br />
<texcode><br />
\defineproperty[my-secrets][layer][state=top]<br />
<br />
I don't tell this to everybody:<br />
%<br />
\startproperty[my-secrets]%<br />
\quotation{\ConTeXt\ goes beyond the limits of imagination, that's why I love it!}<br />
\stopproperty<br />
%<br />
You'll understand that once you get to know it better.<br />
</texcode><br />
<br />
which results in<br />
<br />
<context><br />
\setupcolors[state=start]<br />
\defineproperty[my-secrets][layer][state=top]<br />
<br />
I don't tell this to everybody:<br />
%<br />
\startproperty[my-secrets]%<br />
%<br />
% for the curious eyes: this effect was faked on Wiki as it doesn't work otherwise<br />
% (when PNG is generated from the PDF, the hidden layer becomes visible)<br />
%<br />
\color[white]{\quotation{\ConTeXt\ goes beyond the limits of imagination, that's why I love it!}}<br />
\stopproperty<br />
%<br />
You'll understand that once you get to know it better.<br />
</context><br />
<br />
This can be compared to the non-hidden version, which is:<br />
<br />
<context><br />
I don't tell this to everybody:<br />
{\quotation{\ConTeXt\ goes beyond the limits of imagination, that's why I love it!}}<br />
You'll understand that once you get to know it better.<br />
</context><br />
<br />
== Layer: state=stop ==<br />
<br />
== Transparent fonts ==<br />
<br />
== Final Remarks ==<br />
<br />
* <b>Just as a hint</b>: Don't ever think about hiding your secrets or solutions to the problems for your students that way!<br><br />
If you happen to have Adobe Acrobat Professional, you can select <i>Layers</i> on the left and switch the hidden layer on again.<br><br />
<font color="red">YOU HAVE BEEN WARNED!</font><br />
<br />
* kerning: could cause micro differences in the exact placemet of the (hidden) content<br />
<br />
[[Category:Presentation]]</div>Hajtmar