| 1 |
Variations on the `\expandafter` TeX primitive |
| 2 |
============================================== |
| 3 |
* E-mail: [email protected] |
| 4 |
* Released under the LaTeX Project Public License v1.3c or later |
| 5 |
See http://www.latex-project.org/lppl.txt |
| 6 |
|
| 7 |
The package defines `\multiexpand`, `\multiexpandafter`, `\MultiExpand` |
| 8 |
and `\MultiExpandAfter`, replacing large chains of `\expandafter`. |
| 9 |
These four commands take as an argument the number of expansion to be |
| 10 |
done. If eTeX is available, this number is evaluated using `\numexpr`. |
| 11 |
|
| 12 |
Say we want to expand `\C` five times before `\A` and `\B` in `\A\B\C`. |
| 13 |
The traditional approach would be to insert 31 `\expandafter` before |
| 14 |
`\A` and the same number before `\B`. With this package one can use |
| 15 |
any of |
| 16 |
|
| 17 |
\expandafter\A\expandafter\B\romannumeral\multiexpand{5}\C |
| 18 |
|
| 19 |
\expandafter\A\romannumeral\multiexpandafter{5}\B\C |
| 20 |
|
| 21 |
\MultiExpandAfter{2}\A\MultiExpandAfter{5}\B\C |
| 22 |
|
| 23 |
\MultiExpandAfter{2}\A\MultiExpandAfter{2}\B\MultiExpand{5}\C |
| 24 |
|
| 25 |
In one step of expansion (triggered by the `\expandafter`'s), |
| 26 |
`\romannumeral\multiexpand{5}` expands the following token 5 times, |
| 27 |
whereas `\romannumeral\multiexpandafter{5}` expands the token after |
| 28 |
that 5 times. The macros `\MultiExpandAfter` and `\MultiExpand` |
| 29 |
take two steps of expansion, but do not require `\romannumeral`. |
| 30 |
Another example is that in two steps of expansion, |
| 31 |
|
| 32 |
\MultiExpandAfter{2}\a\MultiExpandAfter{3}{% |
| 33 |
\MultiExpandAfter{9}\b\MultiExpandAfter{10}\c\d} |
| 34 |
|
| 35 |
expands `\d` 10 times, then `\c` 7 times (9-2), then `\b` once (3-2). |
| 36 |
|
| 37 |
The package can be built from the file `multiexpand.dtx' by running |
| 38 |
|
| 39 |
pdflatex multiexpand.dtx |
| 40 |
pdflatex multiexpand.dtx |
| 41 |
|