I often see code of the following form in package implementations (this example is from the LaTeX3 sources):
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname directlua\endcsname\relax
\else
…
\fi
The first line, containing three \expandafters, confuses me. I can only follow this far:
\begingroupstarts a group- The chain of
\expandafters causes\csname directlua\endcsnameto be converted to a control sequence - After this point, the state is that we're in a group and
\expandafter\endgroup\ifx[directlua]\relax…remain to be examined by the macro processor ([directlua]denotes a control sequence) Now the last
\expandafteris processed and\ifxis expanded, then\endgroupends the group. The TeXbook says this on the topic:When an
\if…is expanded, TeX reads ahead as far as necessary to determine whether the condition is true or false; and if false, it skips ahead (keeping track of\if…\finesting) until finding the\else,\or, or\fithat ends the skipped text. Similarly, when\else,\or, or\fiis expanded, TeX reads to the end of any text that ought to be skipped. The “expansion” of a conditional is empty.This would suggest that the arguments to
\if…are evaluated inside the group. But what about code inside the conditional's branches?
If the purpose of the code in question is indeed to evaluate the \if… inside a group, why is it better than just inserting the conditional between \begingroup and \endgroup?