Changes

Jump to navigation Jump to search
m
typo
< '''Prev:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/User Interaction|User Interaction]] >
 
= Assignments =
Assignments are the backbone of ConTeXt. Abhorred by the concept of style file hacking, we took a considerable effort in building a parameterized system. Unfortunately there is a price to pay in terms of speed. Compared to other packages and taking the functionality of ConTeXt into account, the total size of the format file is still very acceptable. Now how are these assignments done.
</texcode>
Some ConTeXt commands take their default setups from others. All commands that are able to provide backgounds backgrounds or rules around some content, for instance, default to the standard command for ruled boxes. In situations like this we can use:
<texcode>
The boolean <code>\ifparameters</code> can be used afterwards to verify that there were assignments in the supplied argument.
= TwoMulti-pass data =
Two-pass data is data Sometimes, typesetting requires information that can be accessed before it has been created. Oneexample is not available until later on in the page counterdocument. It's possible to print An example would be printing the total amount number of pagesonto on the first pageof the document. This The solution is multi-pass data: data that is saved to (in the <code>.tuc</code> file) in one pass, whichis read again during the second run, which typsets the correct valuesand retrieved and used in a later pass. Thissection explains how to define store and retrieve such two-pass dataacross passes.
== Defining a Storing data set ==
To store values to the <codetexcode>% To define a dataset.tuc</code> the command <code>\definedataset [mysetcats]</code> is used to define a container for the data.
% The first anonymous key-value table is saved at point 1 in the cats dataset.% (The dataset is itself a key-value table, too.)\setdataset[cats][name=Gus, job= Storing data Theatre Cat] % The next anonymous table is saved at point 2 % (Regardless of intervening named tables)\setdataset[cats][scene=crime, name=Macavity, is=not there]
% To store key-value pairs save a table by name, add the command <code>name in between the dataset and the table\setdataset</code> is used.[cats][culprit][name=Mungojerry]
<texcode>% Named tables can be overwritten \setdataset [mysetcats] [foo=foo-first, bar=bar-firstculprit] \setdataset [myset] [foo=foo-second, bar=bar-second] \setdataset [myset] [foo=foo-third, barname=bar-thirdRumpleteaser]
</texcode>
Subsequent calls with the same keys to different == Retrieving 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"In contrast to the other mentioned commands,the arguments of ["foo"]="foo-first", }, { ["bar"]="bar-second", ["foo"]="foo-second", }, { ["bar"]="bar-third", ["foo"]="foo-third", }, cmd|datasetvariable},}are delimited by braces instead of</pre>brackets. The reasoning is that this command is about producing text rather than setting values.
<texcode>% Retrieving a named valueThe system takes care name of the culprit was \datasetvariable{cats}{culprit}{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% Retrieving an anonymous value -first, bar=bar-first]\setdataset [myset] [second] [foo=foo-secondMacavity, bar=bar-second]in this case.\setdataset [myset] [third] [foo=foo-third, bar=bar-third]datasetvariable{cats}{2}{name}.
</texcode>
These calls with the additional parameter lead to the following entries in the <code>.tuc</code> file.== Technical properties ==
* The tables of key-value pairs are stored as Lua tables in a <code>.tuc</code> file. They look like this:
<pre>
utilitydata.job.datasets.collected={
["mysetmydata"]={ { -- this table is indexed at position 1 in mydata ["firstkey"]={"value", }, ["barnamed"]={ -- this table is indexed at position "bar-firstnamed",in mydata ["fookey_in_named_table"]="foo-firstvalue",
},
["second"]={ ["bar"]="bar-second" }, ["foomyotherdata"]="foo-second", },{ ["third"]={ -- this table is indexed at position 1 in myotherdata ["barkey"]="bar-third", ["fooothervalue"]="foo-third",
},
},
</pre>
If one is only interested * Adding the option <code>[delay=yes]</code> to a {{cmd|definedataset}} call ensures that the following three entries are present in the last every table saved valueswith {{cmd|setdataset}}: <code>index</code>, <code>order</code>, the and <code>realpage</code>.  ** The entries <code>\setdatasetindex</code> should be used with three argements and <code>order</code> seem to have the second argument should stay same number: a table written by the samefirst {{cmd|setdataset}} call has <code>index=1</code> and <code>order=1</code>, and so on. Tables may be overwritten, of course. Example:
** The entry <code>realpage</code> contains the real page number (so every page is counted, numbered or not) of the page TeX was working on when it encountered the {{cmd|setdataset}} call.
<texcode>
\setdataset [test] [foo] [foo=first]% Create the dataset `mydata`\setdataset [test] definedataset[foomydata] [foodelay=secondyes]
\setdataset [test] [bar] [bar% index=first]\setdataset [test] [bar] [bar1, realpage=second]2\setdataset [testmydata] [bargoldfish] [barcolour=thirdgold]
</texcode>% index=2, realpage=1\setdataset[mydata][silverfish][type=insect]\page[yes]
% index=3, realpage= Retrieving the values =2; overwrites previous goldfish table.\setdataset[mydata][goldfish][type=fish]
The values can be retrieved with % Results in the following Lua table in the <code>\datasetvariable</code> command. The first argument is the data set defined with <code>\definedatasettuc file% ["mydata"]={% ["goldfish"]={% ["type"]="fish",% ["index"]=3,% ["order"]=3,% ["realpage"]=2,% },% ["goldfish"]={% ["type"]="insect",% ["index"]=2,% ["order"]=2,% ["realpage"]=1,% },% }</codetexcode>.
The second argument is a number if the first variant with two parameters of * Keys or values cannot contain commas.<codetexcode>% This does not work\setdataset</code> was used. It's the number of the <code>definedataset[mydata]\setdataset</code> call. If the second variant with three parameters was used[mydata][name="Bond, then it is the identifier given in the second argument.James Bond"]
And the third argument is the key which value should be retrieved.% Results in % ["mydata"]={% {% ["name"]="\"Bond", % ["James Bond\""]="",% },% }</texcode>
Note: In contrast to the other mentioned commands, the arguments of * Single keys are stored as empty values.<codetexcode>\datasetvariable</code> are delimited by bracesdefinedataset[mydata]\setdataset[mydata][A4, instead of brackets, because the actually typeset data. That's ConTeXts convention.landscape]
Complete example% Results in % ["mydata"]={% {% ["A4"]="", % ["landscape"]="",% },% }</texcode>
* Values can store commands; the command is stored as a string, and expanded at the moment of retrieval.
<texcode>
% Create a macro\starttextdef\mytitle{peanut butter}
% Store a command that uses that macro in a variable.\definedataset [myset-1looks]\setdataset[looks][narrower][before=\margintitle{\mytitle}]
% two arguments ⇒ the data is accessed by numberRedefine that macro \setdataset [myset-1] [foo=foo-first, bar=bar-first] def\setdataset [myset-1] [foo=foo-second, bar=bar-second]mytitle{cheese}
% 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 The margintitle is accessed by identifier \setdataset [myset-2] [first] [alpha=alpha-first'cheese', beta=beta-first]not 'peanut butter'. \setdataset [myset-2] [second] setupnarrower[alpha=alpha-second, betabefore=beta-second]  % Outputs alpha-first \datasetvariable{myset-2looks}{firstnarrower} {alphabefore}\par  % Outputs alpha-second \datasetvariable{myset-2}{second}{alpha}\par] % Outputs beta-first \datasetvariable{myset-2}{first} {beta}\parstartnarrower % Outputs beta-second \datasetvariable{myset-2}{second}{beta}\parsandwich\stoptextstopnarrower
</texcode>
 
< '''Prev:''' [[System Macros/Comma Separated Lists|Comma Separated Lists]] | '''Top:''' [[System Macros]] | '''Next:''' [[System Macros/User Interaction|User Interaction]] >
[[Category:System MacrosProgramming and Databases]][[Category:ConTeXt programmingTools]]
138

edits

Navigation menu