I am getting confused about the \expandafter command in TeX. In TeXbook, Knuth said that

\expandafter token. TeX first reads the token that comes immediately after \expandafter, without expanding it; let's call this token t. Then TeX reads the token that comes after t (and possibly more tokens, if that token has an argument), replacing it by its expansion. Finally TeX puts t back in front of that expansion.

Here is an example:

% \b\c->\c->A
\def\a#1{\ifx#1\c c\else not c\fi}

the result is c, not not c, which is not what I expect! Since TeX replace (token that comes after t) by its expansion, why does \c not expand into letter A? How should I understand \expandafter?

  • 4
    \expandafter\a\b\c will expand \b (once!) first which as it reads \c as its argument leads to \a\c (the expansion of \b is \c, not A) so \a gets \c as its argument and compares it to \c and consequently the result is c
    – cgnieder
    Jun 9, 2013 at 12:52

1 Answer 1


The expansion of \b consists in looking for an argument and substituting \b and the argument with the replacement text. So when the \expandafter acts, it does its job and disappears, leaving


in the input stream. This in turn expands to

\ifx\c\c c\else not c\fi

which correctly gives c. If you want to expand also the \c token you have to do differently:


When the first \expandafter acts you remain with


and then the action of the left \expandafter will produce

\a A

which becomes

\ifx A\c c\else not c\fi

that will give not c.

Remember that \expandafter does only the first level expansion, not all the way through.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .