System Macros/Expansion Control

From Wiki
< System Macros
Revision as of 11:50, 25 December 2006 by Taco (talk | contribs) (general updates)
Jump to navigation Jump to search

< Prev: Scratch Variables | Top: System Macros | Next: Handling Arguments >

Expansion control macro shortcuts

When in unprotected mode, to be entered with \unprotect, one can use \@NX as equivalent of \noexpand, and \@EA as equivalent of \expandafter. \@EAEA expands to two expandafters, \@EAEAEA to three, and \@EAEAEAEAEA expands to \@EA\@EAEAEA\@EA. At first sight, these macros are simply shortcuts that are a bit easier to type and read, but there is a bit more to it. Here is an example where \@EA\@EAEAEA\@EA is not the same as five \expandafters in a row:


The two commands \expandoneargafter and \expandtwoargsafter make macros more readable by hiding a lot of \expandafter's. They expand the arguments after the first command.

\expandoneargafter \command{\abc}
\fullexpandoneargafter \command{\abc}

These commands expect the arguments to be macros, the \type{\full} versions do a deep expansion.

Expanding all arguments

Sometimes we pass macros as arguments to commands that don't expand them before interpretation. Such commands can be enclosed by \expanded, like:


Such situations occur for instance when \alfa is a commalist or when data stored in macros is fed to indexing or list generation commands. If needed, one could use \noexpand inside the argument, but some very often occuring problems with expansion (like accents) are intercepted by a somewhat smarter version with the same basic functionality:


The \safe... form is actually so useful that there are safe versions of \edef and \xdef avialable: \safeedef and \safexdef (only usable for definitions that do not need arguments).

Preventing expansion

When expansion of a macro gives problems we can precede it by \unexpanded, like so:

\unexpanded\def\somecommand{... ... ...}

This will prevent the macro from being expanded in places where no typesetting occurs, like when strings are written to the tuo file.

Expansion problems can get quite complex. There are some other internal macros that can help harnassing it, but it is fairly unlikely that you will need them. If you believe you do, read the syst-gen.tex source code.

< Prev: Scratch Variables | Top: System Macros | Next: Handling Arguments >