Difference between revisions of "Command/groupedcommand"

From ConTeXt wiki
Jump to navigationJump to search
(Create entire page)
 
m
Line 32: Line 32:
 
is a robust implementation of the more naïve pattern
 
is a robust implementation of the more naïve pattern
  
</texcode>
+
<texcode>
 
\def\dostuff#1{...before...#1..;after...}
 
\def\dostuff#1{...before...#1..;after...}
 
</texcode>
 
</texcode>

Revision as of 10:08, 10 February 2015

\groupedcommand

Syntax

\groupedcommand{...}{...}
{...} before
{...} after

Description

\groupedcommand is used for defining commands that might receive a verbatim environment as their argument, or a block wrapped in \bgroup...\egroup.

\def\dostuff{\groupedcommand{...before...}{...after...}}

is a robust implementation of the more naïve pattern

\def\dostuff#1{...before...#1..;after...}

The first problem with the naïve approach: the tokens that form #1 are fixed the the moment the argument is read in. Normally this is no problem, but e.g. verbatim environments adapt the catcodes of characters and therefore are not always happy with already fixed tokens.

Another problem arises when the argument is grouped not by {} but by \bgroup and \egroup. Such an argument fails, because the \bgroup is seen as the argument (which is quite normal).

If you use \groupedcommand, the argument is identified and processed correctly.

Example

% Don't write this
\def\cite#1%
  {\rightquote\rightquote#1\leftquote\leftquote}

% Write this, instead.
\def\cite%
  {\groupedcommand{\rightquote\rightquote}{\leftquote\leftquote}}

Another example, where the item to process is a paragraph:

\def\BoldRaggedCenter%
  {\groupedcommand{\raggedcenter\bf}{\par}}

See also

Help from ConTeXt-Mailinglist/Forum

All issues with: