Why does \ifx consider \MakeUppercase{1} and \MakeLowercase{1} different?
You're going to get different results because you're comparing the definitions of the two macros. \MakeUppercase{1}
is not the same as \MakeLowercase{1}
It's worth noting that the \MakeUppercase
and \MakeLowercase
macros are themselves complicated enough that the naïve approaches to trying to expand them to get the results in plain text will still not give expected results (so doing, e.g., \edef
or multiple \expandafter
s won't get you what you might have been hoping for).
Like Donald Hosek said, \ifx
doesn’t expand its arguments. You’re comparing the unexpanded macros \MakeUppercase{1}
and \MakeLowercase{1}
. You could fix this by fully expanding the macros you compare.
However, that doesn’t give you a complete solution, because \MakeUppercase
and \MakeLowercase
are not expandable. So, \edef\upperone{{\MakeUppercase 1}}
will fail. You should instead use the expl3 functions that are fully-expandable, and that also support Unicode.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\ExplSyntaxOn
\edef\upone{\text_uppercase:n {1}}
\edef\lowone{\text_lowercase:n {1}}
\ExplSyntaxOff
\begin{document}
{\upone} and {\lowone} are
\ifx\upone\lowone
identical.
\else
different.
\fi
\end{document}
This gives you, “1 and 1 are identical.”
Edit
Joseph Wright brings up that you might really be after a case-insensitive comparison. For that, you should be using \str_fold_case:n
, which supports Unicode on LuaTeX or XeTeX, but only ASCII on PDFTeX.
The test fails to show equality on (at least) two levels.
First level
\MakeUppercase
and \MakeLowercase
are instructions to print the uppercased or lowercased versions of their arguments. They don't “directly” transform their arguments.
Second level
\ifx
only compares the “surface meaning” of two tokens without any macro expansion. In particular, two macros (every token defined with \def
is a macro) are considered equal by \ifx
if and only if
- they have the same status with respect to
\long
,\outer
and\protected
; - their parameter texts are the same;
- their top level expansions are equal.
In your case subtests 1 and 2 pass, but subtest 3 doesn't, because the top level expansions are
\MakeUppercase{1}
and
\MakeLowercase{1}
respectively, which are different sequences of tokens.
A perhaps simpler example is given by
\def\firstX{X}
\def\secondX{X}
\def\testA{\firstX}
\def\testB{\secondX}
The conditional \ifx\firstX\secondX
will return true, but \ifx\testA\testB
will return false.
Another example: with
\def\first#1{#1}
\def\second#1{#1}
\def\testA{\first{X}}
\def\testB{\second{X}}
the conditional \ifx\testA\testB
will return false because the top level expansions of \testA
and \testB
are different even if ultimately \first{X}
and \second{X}
will deliver the same result. But TeX doesn't look at the “ultimate” effect when doing \ifx
comparisons, just the surface.