Changes

Jump to navigation Jump to search
split section
[[System Macros]]

=== The ConTeXt version ===

<code>\contextversion</code> contains the ConTeXt version string. If you need to make sure you are running under ConTeXt, check for this macro. It is not defined in [[source:syst-gen.tex| syst-gen.tex]] but in [[source:context.tex| context.tex]]. This is because [[source:syst-gen.tex| syst-gen.tex]] is sometimes loaded under Latex.

The expansion of the macro <context code="yes" text="is something like this:">\contextversion</context>

=== Conditional execution of engine-specific code ===

There are a few macrosthat delimit code that is executed conditionally. <code>\beginTEX</code> and <code>\beginETEX</code> are mutually exclusive, depending on whether or not the format file was compiled under an e-TeX-enabled executable. A typical way of setting up your code to use e-TeX where available is like this:

<texcode>
\beginTEX
\def\ifundefined#1%
{\expandafter\ifx\csname#1\endcsname\relax}
\endTEX

\beginETEX \ifcsname
\def\ifundefined#1%
{\unless\ifcsname#1\endcsname}
\endETEX
</texcode>

Code delimited by <code>\beginOMEGA ... \endOMEGA</code> is only executed if ConTeXt runs under Omega, and ignored otherwise.

The optional argument after the <code>\begin...</code> can be used to give information to the viewer: the example above will print the following string to the terminal:

<texcode>
system (E-TEX) : [line 833] \ifcsname
</texcode>

=== Guard against double-loading of input files ===

Because modules can be used in various contexts, we want to be able to prevent macro files from being loaded more than once. This can be done using:

<texcode>
\abortinputifdefined\command
</texcode>

where <code>\command</code> is a command defined in the module to be loaded only once.

For example, [[source:syst-gen.tex| syst-gen.tex]] implements <code>\writestatus</code>, and therefore it starts with:

<texcode>
\abortinputifdefined\writestatus
</texcode>

Actually you don't need this macro for modules, since <code>\usemodule</code> does it's own bookkeeping. It is intended for files that are loaded via the TeX primitive <code>\input</code>.

=== Protecting internal macros ===

We can shield macros from users by using some special characters in their names. Some characters that TeX normally does not consider to be letters (and therefore used) are: <code>@</code>, <code>!</code> and <code>?</code>. Before and after the definition of protected macros, we have to change the <''catcode''> of these characters. This is done by <code>\unprotect</code> and <code>\protect</code>, for instance:

<texcode>
\unprotect
\def\!test{alfa}
\protect
</texcode>

The newly defined command <code>\!test</code> can of course only be called upon when we are in the <code>\unprotect</code>'ed state, otherwise TeX reads the command <code>\!</code>, followed by the word <code>test</code> and probably complains loudly about not being in math mode.

The protection/unprotection commands can be nested (unlike <code>\makeatletter</code> in LaTeX). This nesting is a convenience, since it allows one to use the protection pair regardless of whether protection is already turned on.

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 <code>\unprotect</code> and end them with <code>\protect</code>.

Navigation menu