[texhax] TeX Frequently Asked Questions -- question label "gethelp"

Oleg Katsitadze olegkat at gmail.com
Fri May 2 05:58:30 CEST 2008


On Thu, May 01, 2008 at 11:47:19PM +0100, Steffen van Bakel wrote:
> \def\Group#1{{#1}}
> \newenvironment{mygroup}{\collectbody \Group}{}
> 
> \def\commandA#1(#2){#1(#2)-D}
> \def\commandB#1#2#3{#1({#2(#3)})}
> \def\commandC#1#2#3{#1(\bgroup#2(#3)\egroup)}
> \def\commandD#1#2#3{#1(\begingroup#2(#3)\endgroup)}
> \def\commandE#1#2#3{#1(\begin{mygroup}#2(#3)\end{mygroup})}

[...]

> \expandafter \commandA \commandB A B C    \\  % gives A(B(C))-D
> \expandafter \commandA \commandC A B C \\ % gives A(B(C)-D)
> \expandafter \commandA \commandD A B C \\ % gives A(B(C)-D)
> \expandafter \commandA \commandE A B C \\ % gives A(B(C)-D)

Let's look at the second line.  Imagine \commandC is already expanded
by \expandafter, so you get:

\commandA A(\bgroup B(C)\egroup)
            ^^^^^^^^^^^
            Becomes #2...

...because \bgroup and \egroup do not "wrap up" arguments, TeX does
not nest parentheses like it does braces, and it looks for the closest
argument delimiter, ")" in case of \commandA.  And because \bgroup is
not required to have a matching \egroup in the arguments (unlike
braces), it works without an error (as long as the matching \egroup
follows somewhere).

Does this make sense?  The other lines are similar.

HTH,
Oleg


More information about the texhax mailing list