In TeX, \uppercase{a\lowercase{bC}}
produces Abc
not ABC
, why? As far as I know, TeX will run the inner group before run the outer group, so I think it should be ABC
and even \uppercase{a\expandafter{\lowercase{bC}}}
can't change bc
to BC
. May you tell me the reason?
2 Answers
The \uppercase
and \lowercase
primitives are defined as converting the tokens their input into the appropriate case-mapped output. They perform no expansion and are themselves not expandable (so for example \expandafter{\lowercase{ABC}}
is just the same as {\lowercase{ABC}}
).
It is possible to implement case changing in macros that performs expansion before case changing, or indeed to go all the way and avoid \uppercase
and \lowercase
entirely. This is what expl3
does in \text_uppercase:n
- you get the 'function-like' result but this is a design choice at the macro level.
-
1Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on TeX - LaTeX Meta, or in TeX - LaTeX Chat. Comments continuing discussion may be removed.– Joseph Wright ♦Commented Nov 24, 2023 at 8:00
Your assumption is incorrect. TeX starts from \uppercase
, so it sends the contents down and the stomach regurgitates
A\lowercase{BC}
in the input stream, because the \uppercase
operation acts only on character tokens. So the final result, after doing also \lowercase
is the same as
Abc
had been input to begin with.
\uppercase
applies to the sequence of tokens. TeX provides that sequence. That is, it feeds the primitivea\expandafter{\lowercase{bC}}
but it doesn't otherwise touch them. There's no sense in which it 'runs' the 'inner group' first. Try\uppercase{ab\lowercase{cD}}
\def\cdf{hello}
, the\centerline{\cdf}
can make hello center, so TeX could expand the inner group first. In this post, asexpandafter{\lowercase{bC}}
makes\lowercase{bC}
ran first, so it isbc
that will be feeded into\uppercase
. Where is wrong?