Setups

From Wiki
Revision as of 13:12, 16 May 2013 by Esteis (talk | contribs) (First draft)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Setups

In ConTeXt it is easy to create local variables and grouping. Local variables can be simulated as in:

\startsetups whatever
% some useful definitions here
\stopsetups

\definestartstop[whatever][commands=\setups{whatever}]

\startwhatever
Using definitions here.
\stopwhatever

But 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 {
        \vfill\tfxx\setstrut
        \hsize\overlaywidth
        \hfill Fiona Apple\enspace EM\enspace2005\quad\strut\endgraf
        \kern1ex
    }
\stopsetups

You can even do things like:

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

\start
\setups{settest}
\stop

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

\stoptext

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.

The set-part is called

  • right after the definition of the namespace (initialisation) and
  • after a value is assigned to a variable.

The reset-part is called

  • right after any assignment, but still in front of the set-part.
\setupoutput[pdftex]

\startsetups namespace:set
%
\writestatus{VARIABLES}{namespace:set is beeing called..}%
\ {\green [namespace:set]}
  % whatever must be done with your variables after you assign a value
  %
  % (initialisation with defaults,..)
\stopsetups


\startsetups namespace:reset
%
\writestatus{VARIABLES}{namespace:set is beeing called..}%
\ {\green [namespace:reset]}
  % whatever must be done after an assignment (verification, calculation,..)
\stopsetups


% \setups[namespace:set] is automatically called right after 'set' is assigned
\setvariables[namespace]
  [set={\setups[namespace:set]},
   reset={\setups[namespace:reset]},
  ]

% watch for the colors
\setupcolors[state=start]

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

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

\stoptext