17

As I understand it, the behavior of \expandafter is such that if \a and \b are macros, then \expandafter\a\b will first expand \b then \a.

But my understanding must be wrong, because I don't understand why the following commands

\def\xx{xx}
\expandafter\uppercase{\xx}
\bye

produce "xx" instead of "XX".

After Googling around, it seems that the right way to do this is to replace the second line with

\uppercase\expandafter{\xx}

which indeed works. But \expandafter takes in two arguments; if the first argument is \xx is the first argument, then what is the second?

In any case, since in this case \xx needs to be expanded after \uppercase, why doesn't the first version work -- how can the ordering of the commands be explained?

  • 2
    You need \expandafter\uppercase\expandafter{\xx} so that the second \expandafter "jumps over" the first { to expand \xx. That would leave you with (the expected) \uppercase{xx}. – Werner Sep 6 '14 at 5:28
  • 2
    @Werner \uppercase takes <general text>, so you don't need the first \expandafter. – Joseph Wright Sep 6 '14 at 5:30
  • Isn't it a very frequent question? – wipet Sep 6 '14 at 5:32
  • @wipet I tried googling for this but could not figure it out. I think what tripped me up is the fact (that I just discovered/learned via the responses to this question) is that \uppercase is a primitive that takes "<general text>" as opposed to a macro. – Kevin Sep 6 '14 at 5:37
  • @wipet I can't think of or find a general 'What is the formal syntax of \expandafter?' question or similar, not one about the syntax of \uppercase. Certainly the business with braces and \expandafter does come up, but there's not an obvious duplicate I can see. – Joseph Wright Sep 6 '14 at 6:12
18

The \expandafter primitive expands after exactly one token: it does not take two arguments. As such

\expandafter\uppercase{\xx}

skips over \uppercase and tries to expand {, which is unexpandable and so nothing happens. Thus this is entirely identical to simply writing

\uppercase{\xx}

Thus in general what you have to do to expand the argument after a control sequence is to use two \expandafters

\expandafter\uppercase\expandafter{\xx}

That would be required if \uppercase where a macro, but here we are talking about a primitive. Like several other TeX primitives, \uppercase has a formal syntax which allows expansion before the opening brace. Probably a more common case is something like a toks assignment, where

\def\xx{xx}
\toks0=\expandafter{\xx}
\showthe\toks0

gives

> xx.

The fact that these primitives accept <general text> is allowed is detailed formally in The TeXbook and other reference works (I like TeX by Topic).

  • 1
    Thanks! This, along with your comment above (about \uppercase taking <general text>) answered my question. – Kevin Sep 6 '14 at 5:35
  • \expandafter is not a <filler>, which is a sequence made from space and \relax tokens. The reason why \expandafter is expanded after \uppercase is that TeX is looking for { and expands tokens as it goes, discarding <filler> tokens until finding { or another unexpandable token (with error message). – egreg Sep 6 '14 at 7:58
  • @egreg Are there any primitives that allow \expandafter here but not <filler>? – Joseph Wright Sep 6 '14 at 8:01
  • All primitives (or actions like token register assignments) that want a <general text> share this feature. “Accepting \expandafter” is a consequence. – egreg Sep 6 '14 at 8:03
  • @egreg Yes, but don't they all therefore allow <filler>? – Joseph Wright Sep 6 '14 at 8:06
9

Fully discussing the topic would be really too long, so I'll describe some general facts to give the flavor.

Some TeX primitives require very specific tokens to follow them. To make some examples, I'll mention \over that can be followed by anything legal in math mode and \uppercase that, instead, wants to see after it a <general text>. So in the case of \over, no expansion is made immediately, but only after TeX has started a new math list for the denominator. On the other hand, a <general text> is defined as

<filler>{<balanced text><right brace>

[276] (boldface numbers in brackets will refer to a page of the TeXbook), which implicitly means that \uppercase triggers expansion in order to find { (an explicit or implicit character with category code 1). The <filler> is an arbitrary sequence of space and \relax tokens that will be removed. An example of this is [374]

\uppercase\expandafter{\romannumeral\n}

that produces the representation of the value stored in \n in uppercase Roman numerals. Another example is in Get the lion to run in loops. Tersely.

Another example is \left that the syntax rules require to be followed by a <delim> [292], which in turn is [289]

<delim> → <filler>\delimiter<27-bit number> | <filler><letter> | <filler><otherchar>

(in this case, the <letter> or the <otherchar> should have a nonnegative \delcode, of course). Thus something like \left\space[ will work, because \left is looking for a specific token (either \delimiter or a character token with category code 11 or 12) and will expand \space and then ignore the result which fits as <filler>. A similar situation happens with _ and ^ (see Why does `x_\text y` work?).

One should always have in mind the list of places where TeX doesn't expand tokens, which is found at [215]. So, for instance, TeX will not expand tokens when doing \def or \let. The syntax of \let allows <one optional space> after the (optional) = (more precisely <equals>), but it doesn't do expansion: so

\let\foo=\space\relax

will make \foo equivalent to \space, not to \relax. This is because \let can have any token after <equals>, not a specific one.

The “specific token” required by a primitive can be fairly general; for instance, \the allows a very large set of tokens after it, and it will expand tokens looking for them. The construction

\toks@=\expandafter{\the\toks@ x}

will append x to the previous contents of \toks@. What if we want to append the contents of another token register, say \mytoks? Easy:

\toks@=\expandafter{\the\expandafter\toks@\the\mytoks}

Explanation: the outer \expandafter will trigger the first \the; this action is performed because in a token register assignment TeX is looking for {, so it expands tokens as it goes; now \the wants to see a specific token, so it expands in order to find it; since \expandafter is expandable (with empty expansion), it triggers it, which will expand the second \the. A more complicated version is

\edef\x{\the\toks@\the\mytoks}\toks@=\expandafter{\x}

or

\begingroup\edef\x{\endgroup\toks@={\the\toks@\the\mytoks}}\x

(the latter will not leave a definition for \x).

The construction \expandafter\uppercase{\xx} will do nothing different from \uppercase{\xx} because \expandafter will try to expand {, which is unexpandable. There is no general way to force a one level expansion of more than one token, which wouldn't make much sense anyway. If you have to uppercase a list of characters possibly hidden in macros, then only full expansion makes sense:

\begingroup\edef\x{\endgroup\uppercase{<tokens>}}\x

(again for not leaving behind a definition for \x) or

\begingroup\edef\x{<tokens>}\uppercase\expandafter{\expandafter\endgroup\x}

A simpler command using a scratch macro would be

\edef\next{<tokens>}\uppercase\expandafter{\next}

but I'd not recommend it (troubles are behind the corner if by chance \next had previously be \let to an unexpandable token). In LaTeX one should use \protected@edef, instead of \edef.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.