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

Steffen van Bakel svb at doc.ic.ac.uk
Fri May 2 10:50:10 CEST 2008


Hi Oleg,

Thanks for your reply.

>> \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?

Yes, in part; disappointing, however: how can you automatically  
generate recursive structures that you want to match against (in the  
functional programming style) if not by using \bgroup and \egroup?   
And they are both 'let' to { and } ... but I understand they are not  
the same.

Can you suggest a way around this?

> The other lines are similar.

What's puzzling me is that even the last alternative goes wrong.   
Following your argument, should TeX not complain about the missing  
"\end{mygroup}" when it closes on the first ")"?

Steffen


More information about the texhax mailing list