LaTeX: if, string, uppercase

Following through with the survey of Insights into TEX Macro Writing Techniques (1990), an example combining the commands in the subject line. There is a trick for \uppercase as it is not an expandable macro (what?).

The theory

The above reference says:

\if expands whatever immediately follows it until it arrives at two unexpandable tokens. It then compares them to see if their charcodes match. This test is useful to see if a given letter is upper- or lower-case.

\string isa TEXprimitive which causes the control sequence following it to be broken into a list of character tokens in order to print the control sequence or to process it with another macro. \tt\string\TeX will produce \TeX.

*.tex

\def\aa{LL}
\edef\bb{\uppercase{l}L}
\uppercase{\edef\cc{lL}} % oddly enough

\def\foo[#1]{{\tt\string#1} expands to #1\if #1 and \else \ \emph{but} \fi the pair of charcodes \if #1\relax \else don't \fi match}

\noindent
\foo[\aa]. \foo[\bb]. \foo[\cc].

Output

\aa expands to LL and the pair of charcodes match. \bb expands to LL but the pair of charcodes don’t match. \cc expands to LL and the pair of charcodes match.

Remarks

  • Here‘s where I think I came across the trick for \uppercase.
  • On \edef: this primitive does a full expansion of what is given as it’s argument (in contrast to \def, which simply stores the input)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s