The short answer
Don't think of 11, but of 7+3+1 \expandafter
s. To also reach the \e
, you'll need 15+7+3+1 \expandafter
s. As Ulrich says in his comment, these number are 2^4-1
, 2^3-1
and so on.
What does \expandafter
do?
The TeX primitive \expandafter
steps over the token following it and once expands the next one. Since your macros \b
, \c
and \d
don't take arguments, the code
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\a
\expandafter\expandafter\expandafter\b\expandafter\c\d\e
expands to
\expandafter\expandafter
\expandafter\a
\expandafter\b\c "\d, once expanded" \e
and this in turn to
\expandafter\a\b "\c, once expanded" "\d, once expanded" \e
to finally yield
\a "\b, once expanded" "\c, once expanded" "\d, once expanded" \e
With your definition of \a
, \b
and \c
, you don't need all these \expandafter
s since those macros don't take any arguments. In fact, the above is an oversimplification: In the general case, if the macros do take arguments, the last line above would look somewhat like
\a \once{\b \once{\c \once{\d \e}}}
where \once
is not a TeX command but should mean that the argument is expanded once, and the innermost expansion happens first, the outermost last. Here's a really stupid example:
\def\a#1.{#1}
\def\b#1:{#1.}
\def\c#1,{#1:}
\def\d{Hello world!,}
\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\a
\expandafter\expandafter\expandafter\b
\expandafter\c\d
\bye
\def
Firstly, \c
replaces the ,
in Hello world!,
with :
. Secondly, \b
replaces the :
with .
, and finally \a
strips the .
. In this example, all the \expandafter
s are needed.
What do 2^n-1
\expandafter
s do?
2^1-1=1
\expandafter
expands the stuff after the next token once, and by induction you easily see that 2^n-1
\expandafter
s expand the stuff after the next token n
times. Moreover, making this happen takes n
expansion steps. Let's look again at your code, now from this point of view:
The first 7 \expandafter
s expand the stuff after \a
3 times. Two of these 3 expansion steps are needed to expand the 3 \expandafter
s after \a
; let's keep in mind that a last expansion step is missing. Before that last step, the 3 \expandafter
s after \a
expand the stuff after \b
2 times. The first expansion yields \c \once{\d \e}
after the \b
, the second one yields \once{\c \once{\d \e}}
. This completes the description of two of these 3 expansion steps. In the third of these 3, the stuff after \a
is expanded one last time.
In TH's comment, the 3 \expandafter
s expand the \csname bar\endcsname
twice, thus yielding the expansion of \bar
. Another important case is
\expandafter\def\expandafter\mycommand\expandafter{\somecontrolsequence}
This defines \mycommand
as \somecontrolsequence
, once expanded.
Another stupid example:
\expandafter\expandafter\expandafter\a
\expandafter\expandafter\expandafter\b\c
expands \c
twice, thus expanding to something like
\a\b\twice{\c}
\expandafter\SomeToken\superexpandafter{n}
. You can't get the\superexpandafter
to work 'as expected' here, as it would require at least two expansions to continue the chain past\SomeToken
.%
.