Inside ConTeXt

From Wiki
Revision as of 20:20, 4 July 2005 by 141.84.30.3 (talk)
Jump to navigation Jump to search

< Main Page

Using variables

\setvariables[namespace][key=value]
\getvariable{namespace}{key}

Defining new commands

Special characters in command names

Some commands have special characters in their names, that TeX normally does not consider to be letters: @, ! and ?. Before and after the use or definition of such protected commands in your input files, the catcode of these characters has to be changed. This is done by \unprotect and \protect:

\unprotect
\def\!test{alfa} 
\protect 

The newly defined command \!test can of course only be called upon when we are in the \unprotected state, otherwise TeX reads the command \!, followed by the word test (and probably complains loudly about not being in math mode). These protection/unprotection commands can be nested. When the nesting becomes deeper than one level, the system reports the current protection level. It is a good habit to always start your macro files with \unprotect and end them with \protect.

See also: Commands with Key=Value arguments, Commands with optional arguments >

Processing a comma-separated list of values

Suppose you defined a command like this one somewhere in your document:

\def\IHaveTo#1#2{I have to #1 on #2.\par}

So calling

\IHaveTo{tidy up}{Monday}

Will print out

I have to tidy up on Monday.

But sometimes you have to repeat some task more than once. In this case you can define a new command:

\def\MyMumOrderedMeTo[#1]#2%
  {\processcommalist[#1]{\IHaveTo{#2}}}

Calling

\MyMumOrderedMeTo[Monday,Wednesday,Saturday]{tidy up}

will spare you some typing (however not tidying up!):

I have to tidy up on Monday.
I have to tidy up on Wednesday.
I have to tidy up on Saturday.

In case a command \IHaveTo is already defined in a slightly different way:

\def\IHaveTo[#1]#2{I have to #2 on #1.\par}

you can define \MyMumOrderedMeTo as:

\def\MyMumOrderedMeTo[#1]#2%
  {\begingroup
   \def\processitem##1{\IHaveToDo[##1]{#2}}%
   \processcommalist[#1]\processitem
   \endgroup}