4

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?

Y. zeng
  • 1,885
  • Why do you think 'TeX will run the inner group before ... the outer group'? As far as I know, that's false. \uppercase applies to the sequence of tokens. TeX provides that sequence. That is, it feeds the primitive a\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}} – cfr Nov 24 '23 at 06:45
  • @cfr For example, \def\cdf{hello}, the \centerline{\cdf} can make hello center, so TeX could expand the inner group first. In this post, as expandafter{\lowercase{bC}} makes \lowercase{bC} ran first, so it is bc that will be feeded into \uppercase. Where is wrong? – Y. zeng Nov 24 '23 at 06:48
  • Because it doesn't expand the inner group first. If you think that only because it would explain some cases, well, that's a flawed methodology ;). If that was the rule, you'd get a centred hello and BC. But that's not what TeX does. So it's a bad way to predict results. – cfr Nov 24 '23 at 06:52
  • TeX does different things. But here, TeX eats tokens. It reads the tokens in order from the input stream and acts on those tokens according to the set of instructions it has. In the case of \uppercase{a\expandafter{\lowercase{bC}}, TeX finds \uppercase first. The instructions say to flip tokens it finds between the following {}. So it flips them. If it worked, you'd get A\expandafter{\lowercase{BC}}. But you actually get an error. – cfr Nov 24 '23 at 06:58
  • @cfr So, TeX doesn't eat them form innermost, but far left to far right? \uppercase{a\lowercase{bC}} to A\lowercase{bC} to Abc? – Y. zeng Nov 24 '23 at 07:04
  • @cfr Why does \expandafter in \uppercase\expandafter{\romannumeral1986} works? – Y. zeng Nov 24 '23 at 07:11
  • \romannumetsl is defined to be expandable – David Carlisle Nov 24 '23 at 07:19
  • @DavidCarlisle What is the meaning? May you explain it with more details? – Y. zeng Nov 24 '23 at 07:24
  • 2
    https://tex.stackexchange.com/questions/467360/is-there-a-list-of-expandable-tex-primitives-latex-e-tex-others/467372#467372 – David Carlisle Nov 24 '23 at 09:04
  • 1
    @Y.zeng Yes. From left to right. – cfr Nov 24 '23 at 15:20

2 Answers2

7

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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    Comments 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 [meta], or in [chat]. Comments continuing discussion may be removed. – Joseph Wright Nov 24 '23 at 08:00
6

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.

egreg
  • 1,121,712