System Macros/Key Value Assignments

From Wiki
Jump to navigation Jump to search

< Prev: Comma Separated Lists | Top: System Macros | Next: User Interaction >


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.

Assignments can be realized with:




These macros are a syntactic rewrite for the 'set', 'clear' and 'initialize' actions:

\def\labelvariable{value}      % \doassign
\def\labelvariable{}           % \undoassign
   {\def\labelvariable{value}} % \doassignempty

Using the assignment commands directly is not our ideal of user friendly interfacing, so we take some further steps.

\getparameters [label] [...=...,...=...]

Again, the label identifies the category a variable belongs to. The second argument can be a comma separated list of assignments. Duplicates are allowed, later appearances overrule earlier ones.


is equivalent to


Some explanation of the inner workings of ConTeXt is in order here to make sure the source is understandable for readers, since the actual internal usage is a bit more complex than this.

In the pre-multi-lingual (simple) stadium ConTeXt took the next approach. With these definitions (that are mainly there to conserve TeX's string space):

\def\??demo {@@demo}
\def\!!width {width}
\def\!!height {height}



lead to:


Because we want to be able to distinguish the !! pre-tagged user supplied variables from internal counterparts, we will introduce a slightly different tag in the multi-lingual modules. There we will use c! or v!, depending on the context.

The call will typically somewhat look like this:


In the dutch interface, this would (e.g.) expand into:


but in the english interface it would become:


ConTeXt continues to function, because it never uses the explicit expansion, anywhere. It always relies on the \s! and \v! definitions (remember I told you not to touch them? this is why.)

Sometimes we explicitly want variables to default to an empty string, so we welcome:

\getemptyparameters [label] [...=...,...=...]

Some ConTeXt commands take their default setups from others. All commands that are able to provide backgrounds or rules around some content, for instance, default to the standard command for ruled boxes. In situations like this we can use:

\copyparameters [to-label] [from-label] [name1,name2,...]

For instance


Leads to (english version):

\def\@@internalwidth  {\@@externalwidth}
\def\@@internalheight {\@@externalheight}

A lot of ConTeXt commands take optional arguments, for instance:


Although a combined solution is possible, we prefer a separation between argument keywords and parameter assignments. The next command takes care of propper handling of such multi-faced commands.

\doifassignmentelse {...} {then ...} {else ...}

A slightly different approach is \checkparameters, which also checks on the presence of a =, just like the previous macro.


The boolean \ifparameters can be used afterwards to verify that there were assignments in the supplied argument.

Multi-pass data

Sometimes, typesetting requires information that is not available until later on in the document. An example would be printing the total number of pages on the first page of the document. The solution is multi-pass data: data that is saved (in the .tuc file) in one pass, and retrieved and used in a later pass. This section explains how to store and retrieve such data across passes.

Storing data

% To define a dataset.

% 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=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 save a table by name, add the name in between the dataset and the table

% Named tables can be overwritten

Retrieving data

In contrast to the other mentioned commands, the arguments of \datasetvariable are delimited by braces instead of brackets. The reasoning is that this command is about producing text rather than setting values.

% Retrieving a named value
The name of the culprit was \datasetvariable{cats}{culprit}{name}.

% Retrieving an anonymous value --- Macavity, in this case.

Technical properties

  • The tables of key-value pairs are stored as Lua tables in a .tuc file. They look like this:
  {                  -- this table is indexed at position 1 in mydata
  ["named"]={        -- this table is indexed at position "named" in mydata
  {                  -- this table is indexed at position 1 in myotherdata
  • Adding the option [delay=yes] to a \definedataset call ensures that the following three entries are present in every table saved with \setdataset: index, order, and realpage.
    • The entries index and order seem to have the same number: a table written by the first \setdataset call has index=1 and order=1, and so on. Tables may be overwritten, of course.
    • The entry realpage contains the real page number (so every page is counted, numbered or not) of the page TeX was working on when it encountered the \setdataset call.
% Create the dataset `mydata`

% index=1, realpage=2

% index=2, realpage=1

% index=3, realpage=2; overwrites previous goldfish table.

% Results in the following Lua table in the .tuc file
% ["mydata"]={
%  ["goldfish"]={
%   ["type"]="fish",
%   ["index"]=3,
%   ["order"]=3,
%   ["realpage"]=2,
%  },
%  ["goldfish"]={
%   ["type"]="insect",
%   ["index"]=2,
%   ["order"]=2,
%   ["realpage"]=1,
%  },
% }
  • Keys or values cannot contain commas.
% This does not work
\setdataset[mydata][name="Bond, James Bond"]

% Results in 
% ["mydata"]={
%  {
%   ["name"]="\"Bond", 
%   ["James Bond\""]="",
%  },
% }
  • Single keys are stored as empty values.

% Results in 
% ["mydata"]={
%  {
%   ["A4"]="", 
%   ["landscape"]="",
%  },
% }
  • Values can store commands; the command is stored as a string, and expanded at the moment of retrieval.
% Create a macro
\def\mytitle{peanut butter}

% Store a command that uses that macro in a variable.

% Redefine that macro

% The margintitle is 'cheese', not 'peanut butter'.

< Prev: Comma Separated Lists | Top: System Macros | Next: User Interaction >