starttexdefinition

From ConTeXt wiki

\starttexdefinition

Syntax

\starttexdefinition ... ... ... command ...
... global (optional)
... unexpanded (optional)
... expanded (optional)
... #arguments


Description

The starttexdefinition uses the start…stop syntax to define new control sequences. They directly map to the brace-delimited definitions. It's also possible to define commands that take arguments. Both, the numbered syntax and the MkVI name-syntax are supported. To use MkVI syntax, save the file as file.mkvi or place the line %% macros=mkvi at the beginning of the document. The command features several modifier sequences, which are demonstrated in the following examples.

Example

%% \def\command{…}
\starttexdefinition command
	…
\stoptexdefinition

%% \def\command#1#2{…}
\starttexdefinition command #1 #2
	…
\stoptexdefinition

%% \def\command#name#options{…}
\starttexdefinition command #name #options
	…
\stoptexdefinition

%% \unexpanded\def\command#name#options{…}
\starttexdefinition unexpanded command #name #options
	…
\stoptexdefinition

%% \gdef\command{…}
\starttexdefinition global command
	…
\stoptexdefinition

%% \unexpanded\gdef\command{…}
\starttexdefinition global unexpanded command
	…
\stoptexdefinition

%% \unexpanded\edef\command#arg{…}
\starttexdefinition global unexpanded command #arg
	…
\stoptexdefinition

%% \unexpanded\xdef\command#arg{…}
\starttexdefinition global unexpanded expanded command #arg
	…
\stoptexdefinition

Pitfalls

The order in which the modifier sequences global, unexpanded and expanded appear matters. One difference between traditional definitions and the start-stop environment is that spaces are ignored in the texdefinition environment, so line endings don't have to be littered with percent signs and the definitions tend to be more readable. This is usually what is desired when writing macros. However, it has to be taken into account when encapsulating Lua code.

%% no problems here
\def\somecommand
  {\startluacode
    if true then
    else
    end
  \stopluacode}

\somecommand

%% this will fail
\starttexdefinition somecommand
  \startluacode
    if true then
    else
    end
  \stopluacode
\stoptexdefinition

\somecommand

The above texdefinition example will fail, since the line endings, which normally result in a space are ignored and the spaces collapsed, so Lua receives if true thenelseend, which is not valid. The solution is to add a space and place a percent sign, which will not be removed in that case.

%% this works
\starttexdefinition somecommand
  \startluacode
    if true then %%
    else %%
    end
  \stopluacode
\stoptexdefinition

\somecommand

See also

Help from ConTeXt-Mailinglist/Forum

All issues with: