System Macros/Key Value Assignments

From Wiki
< System Macros
Revision as of 09:28, 1 August 2006 by Taco (talk | contribs) (split off section)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

System_Macros

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:

\doassign[label][variable=value]
\undoassign[label][variable=value]

and:

\doassignempty[label][variable=value]

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

\def\labelvariable{value}      % \doassign
\def\labelvariable{}           % \undoassign
\doifundefined{\labelvariable}
   {\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.

\getparameters
  [demo]
  [alfa=1,
   beta=2]

is equivalent to

\def\demoalfa{1}
\def\demobeta{2}

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}

calling

\getparameters
  [\??demo]
  [\!!width=one,
   \!!height=2]

lead to:

\def\@@demowidth{one}
\def\@@demoheight{2}

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:

\getparameters
  [\??demo]
  [\c!width=\v!one,
   \c!height=2]

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

\def\@@demobreedte{een}
\def\@@demohoogte{2}

but in the english interface it would become:

\def\@@demowidth{one}
\def\@@demoheight{2}

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 backgounds 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

\copyparameters
  [\??internal][\??external]
  [\c!width,\c!height]

Leads to (english version):

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

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

\dothisorthat[alfa,beta]
\dothisorthat[first=foo,second=bar]
\dothisorthat[alfa,beta][first=foo,second=bar]

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.

\checkparameters[argument]

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