What are the values for parameters of an \edef at the time of definition?
I would say that it compares #
with 1
hance always goes for the false branch. (Compares second test where it compares #
with #
and chooses true branch)
\edef\bar#1{\if#1#1 (TRUE)\else #1 (FALSE)\fi}
\show\bar
\edef\bar#1{\if##1 (TRUE)\else (FALSE)\fi}
\show\bar
\bye
Gives
> \bar=macro:
#1->#1 (FALSE).
l.3 \show\bar
> \bar=macro:
#1->1 (TRUE).
l.8 \show\bar
When TeX does \edef<macro><parameter text>{
1<replacement text>}
2 it sets aside the <macro>
the replacement text and {
1, then does full expansion to <replacement text>
until finding the matching }
2. Finally it does
\def<macro><parameter text>{<full expansion of the replacement text>}
The full expansion of \if#1XX\else not X\fi
is not X
, because #
and 1
are different character tokens (by character code).
Thus your \edef
becomes the same as
\def\bar#1{not X}
As another example,
\edef\rightbracestring{\string}}
is perfectly legal, even if it appears to have unbalanced braces, because when the expansion is being done, the first }
has already been tokenized as }
12 when encountered. This proves that TeX doesn't first absorb the whole <replacement text>
, but performs expansion just like in normal circumstances, with the difference that the matching }
2 stops the process and resumes the assignment.