Changes

Jump to navigation Jump to search
3,903 bytes added ,  23:54, 11 October 2013
→‎Multipass: begin new section
[http://www.ntg.nl/pipermail/ntg-context/2012/066940.html this]
thread on the mailing list.
 
 
== Multipass ==
 
In some circumstances, portions of code are evaluated two or more
times.
This can be disruptive in contexts where the order of actions is
important.
 
=== Trialtypesetting ===
 
Often the size of elements must be calculated prior to determining the
optimal placements.
Probably the most common example are tables:
In order to align cells correctly the dimensions of later parts of the
document must already be determined before anything is typeset.
The stage during which this takes place is called *trial typesetting*.
 
The system mode ''trialtypesetting'' allows fine-grained control over
what code should be executed during what stage.
For instance, the naive definition of a macro that increments and
prints a counter register will behave erratically in tables:
 
<context source="yes" mode="mkiv">
\def \inccount {%
\incrementnumber [somecount]%
\getnumber [somecount]%
}
\definenumber [somecount]
\setnumber [somecount] [42]
 
\starttext
 
before \inccount
 
\startplacetable[location=here]
\bTABLE
\bTR \bTD \inccount \eTD \bTD \inccount \eTD \eTR
\eTABLE
\stopplacetable
 
after \inccount
 
\stoptext
</context>
 
To bypass the extra evaluation, wrap the incrementation part in an
<code>\iftrialtypesetting</code> conditional.
 
<context source="yes" mode="mkiv">
\def \inccount {%
\iftrialtypesetting \else
\incrementnumber [somecount]%
\fi
\getnumber [somecount]%
}
 
\definenumber [somecount]
\setnumber [somecount] [42]
 
\starttext
 
before \inccount
 
\startplacetable[location=here]
\bTABLE
\bTR \bTD \inccount \eTD \bTD \inccount \eTD \eTR
\eTABLE
\stopplacetable
 
after \inccount
 
\stoptext
</context>
 
 
Note that annoying as it may appear, the trial typesetting phase is
essential for certain features to work, so take extra care when
omitting it.
In the example above the number itself must be present as placeholder
during the first pass even though it has not been updated at this
point.
 
<texcode>
\def \inccount {%
\iftrialtypesetting \else
\incrementnumber [somecount]%
\getnumber [somecount]% <= wrong!
\fi
}
</texcode>
 
<context mode="mkiv" source="no">
\def \inccount {%
\iftrialtypesetting \else
\incrementnumber [somecount]%
\getnumber [somecount]%
\fi
}
 
\definenumber [somecount]
\setnumber [somecount] [42]
 
\starttext
 
before \inccount
 
\startplacetable[location=here]
\bTABLE
\bTR \bTD \inccount \eTD \bTD \inccount \eTD \eTR
\eTABLE
\stopplacetable
 
after \inccount
 
\stoptext
</context>
 
 
=== Tables ===
 
In addition to trial typesetting Context also knows a ''table'' state:
 
<context mode="mkiv" source="yes">
\def \tablecheck {\ifintable In table. \else Outside table. \fi}
 
\starttext
\tablecheck
\startplacetable[location=here]
\bTABLE
\bTR \bTD \tablecheck \eTD \bTD \tablecheck \eTD \eTR
\eTABLE
\stopplacetable
\tablecheck %% decrement
\stoptext
</context>
 
=== Metapost ===
 
As with TeX, Metapost sometimes requires multiple passes,
especially when processing text.
In below snippet, the Metapost tracer reveals that the code is actually
evaluated twice:
 
<texcode>
\enabletrackers[metapost.showlog]
\starttext
\startMPcode
show "This gets printed twice.";
label (btex Some label text etex, (0,0));
\stopMPcode
\stoptext
</texcode>
 
NB removing the <code>label</code> statement will result in the second
pass being omitted.
 
In [[Metafun]], the default Metapost format in Context, the booleans
<code>mfun_first_run</code> and <code>mfun_trial_run</code> allow
detecting the individual stages:
 
<texcode>
\enabletrackers[metapost.showlog]
\starttext
\startMPcode
if mfun_trial_run :
show "This gets printed during the trial pass.";
else :
show "This gets printed during the final pass.";
fi;
label (btex Some label text etex, (0,0));
\stopMPcode
\stoptext
</texcode>
188

edits

Navigation menu