Changes

Jump to navigation Jump to search
Add section about two-pass data
The boolean <code>\ifparameters</code> can be used afterwards to verify that there were assignments in the supplied argument.
 
= Two-pass data =
 
Two-pass data is data that can be accessed before it has been created. One
example is the page counter. It's possible to print the total amount of pages
onto the first page. This data is saved to the <code>.tuc</code> file, which
is read again during the second run, which typsets the correct values. This
section explains how to define and retrieve such two-pass data.
 
== Defining a data set ==
 
To store values to the <code>.tuc</code> the command <code>\definedataset [myset]</code> is used to define a container for the data.
 
== Storing data ==
 
To store key-value pairs the command <code>\setdataset</code> is used.
 
<texcode>
\setdataset [myset] [foo=foo-first, bar=bar-first]
\setdataset [myset] [foo=foo-second, bar=bar-second]
\setdataset [myset] [foo=foo-third, bar=bar-third]
</texcode>
 
Subsequent calls with the same keys to different data like in the example above does not overwrite the former values. All values are stored and enumerated. The calls above lead to the following entries in the <code>.tuc</code> file.
 
<pre>
utilitydata.job.datasets.collected={
["myset"]={
{
["bar"]="bar-first",
["foo"]="foo-first",
},
{
["bar"]="bar-second",
["foo"]="foo-second",
},
{
["bar"]="bar-third",
["foo"]="foo-third",
},
},
}
</pre>
 
The system takes care of the name of the table not to interfere with core tables.
 
One can also add another parameter to address the individual values (with the same keys) from subsequent calls.
 
<texcode>
\setdataset [myset] [first] [foo=foo-first, bar=bar-first]
\setdataset [myset] [second] [foo=foo-second, bar=bar-second]
\setdataset [myset] [third] [foo=foo-third, bar=bar-third]
</texcode>
 
These calls with the additional parameter lead to the following entries in the <code>.tuc</code> file.
 
<pre>
utilitydata.job.datasets.collected={
["myset"]={
["first"]={
["bar"]="bar-first",
["foo"]="foo-first",
},
["second"]={
["bar"]="bar-second",
["foo"]="foo-second",
},
["third"]={
["bar"]="bar-third",
["foo"]="foo-third",
},
},
}
</pre>
 
If one is only interested in the last saved values, the <code>\setdataset</code> should be used with three argements and the second argument should stay the same. Example:
 
<texcode>
\setdataset [test] [foo] [foo=first]
\setdataset [test] [foo] [foo=second]
 
\setdataset [test] [bar] [bar=first]
\setdataset [test] [bar] [bar=second]
\setdataset [test] [bar] [bar=third]
 
</texcode>
 
== Retrieving the values ==
 
The values can be retrieved with the <code>\datasetvariable</code> command. The first argument is the data set defined with <code>\definedataset</code>.
 
The second argument is a number if the first variant with two parameters of <code>\setdataset</code> was used. It's the number of the <code>\setdataset</code> call. If the second variant with three parameters was used, then it is the identifier given in the second argument.
 
And the third argument is the key which value should be retrieved.
 
Note: In contrast to the other mentioned commands, the arguments of <code>\datasetvariable</code> are delimited by braces, instead of brackets, because the actually typeset data. That's ConTeXts convention.
 
Complete example
 
<texcode>
\starttext
 
\definedataset [myset-1]
 
% two arguments ⇒ the data is accessed by number
\setdataset [myset-1] [foo=foo-first, bar=bar-first]
\setdataset [myset-1] [foo=foo-second, bar=bar-second]
 
% Outputs foo-first
\datasetvariable{myset-1}{1}{foo}\par
 
% Outputs foo-second
\datasetvariable{myset-1}{2}{foo}\par
 
% Outputs bar-first
\datasetvariable{myset-1}{1}{bar}\par
 
% Outputs bar-second
\datasetvariable{myset-1}{2}{bar}\blank
 
 
\definedataset [myset-2]
 
% three arguments ⇒ the data is accessed by identifier
\setdataset [myset-2] [first] [alpha=alpha-first, beta=beta-first]
\setdataset [myset-2] [second] [alpha=alpha-second, beta=beta-second]
 
% Outputs alpha-first
\datasetvariable{myset-2}{first} {alpha}\par
 
% Outputs alpha-second
\datasetvariable{myset-2}{second}{alpha}\par
 
% Outputs beta-first
\datasetvariable{myset-2}{first} {beta}\par
 
% Outputs beta-second
\datasetvariable{myset-2}{second}{beta}\par
 
\stoptext
</texcode>
 
< '''Prev:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/User Interaction|User Interaction]] >
69

edits

Navigation menu