From Wiki
Revision as of 19:12, 7 November 2013 by Huttarl (talk | contribs) (corrected typo: \startsetup is not defined)
Jump to navigation Jump to search

In ConTEXt, setups are a rather common variant on macros. Setups have two useful properties:

  • Spaces inside setups are ignored. This means that you can format the setup's contents with spaces and linebreaks for maximum readability, instead of having to write a dense macro or, worse, a long in-place definition. You can explicitly request spaces with \space, \crlf, \par, etc.
  • If you call the setup inside a \start (just 'start') environment, any definitions made by that setup remain inside that group. This lets you define macros in a setup, and then call the setup inside a group to use the macros as 'local macros' of sorts.



Here are two simple examples:

% Create two setups
\startsetups doc:print

\startsetups doc:screen

% Use one or another setup
\doifmodeelse {paper} {
} {
% Set up a headertext. Whitespace is ignored

% Use the setup

You can place setups almost everywhere and environment will not be affected by their execution. It is useful to wrap overlay definitions and such in setups as in (copied from Colorful_CD_Inlay page):

\defineoverlay [origin] [\setups{origin}]

\startsetups origin
    \vbox to \overlayheight {
        \hfill Fiona Apple\enspace EM\enspace2005\quad\strut\endgraf

You can even do things like:

\startsetups settest
    \def\command{do something with}
    I want to \command{} my command.


\command aaa  % will give "undefined control sequence" error


Using setups for namespaces

Using \setups for a variable namespace allows an easier control over the containing variables. All you have to do is to define the setups namespace:set and/or namespace:reset for a given namespace. Now every time a variable of that namespace is assigned (written), ConTeXt automatically calls these setups. Reading of variables is totally unaffected by these settings. A possible use are default values, calculations and even verification.

So once you have 'setup' your variables proper, you don't have to worry about unset variables and alike any more. Also changes can be made easy, as there is only one common setup. The drawback is the slower speed in use, as every assignment to a variable calls these setups.

To give you the idea, try this example.

  • When you initialize the namespace, the setups assigned to set are called.
  • When you assign a variable to the namespace, first the setup in reset is called, and then the setup in set.
\startsetups namespace:set
    % Print message to log and to document
    \writestatus{VARIABLES}{namespace:set is being called..}%
    % whatever must be done with your variables after you assign a value
    % (initialisation with defaults,..)

\startsetups namespace:reset
  % Print message to log and document
  \writestatus{VARIABLES}{namespace:set is being called..}%

  % whatever must be done after an assignment (verification, calculation,..)

% \setups[namespace:set] is automatically called right after 'set' is assigned
   key={Apples are \applecolor!},

% watch for the colors

{\bf reading has no effect\par}
{\tt Calling \type{\getvariable{namespace}{key}}...\getvariable{namespace}{key}}

{\bf writing calls reset and set\par}
{\tt Calling \type{\setvariables[namespace][key=value]}...\setvariables[namespace][key=value]}