[Tex/LaTex] \uppercase in \newcommand

capitalizationmacrosstrings

I tried to use \uppercase in \newcommand:

\newcommand\universidad{My University}
\newcommand\Universidad{\uppercase{\universidad}} 

When I use it I get this,

My University

and not, as expected, this

MY UNIVERSITY

Why?

EDIT

I can't use \MakeUppercase because I need use latin quote, example: ingeniería. And this latin case has problems with \MakeUppercase

Best Answer

With both latin1 and utf8 encodings I get correct output from

\newcommand{\facultad}{Ingeniería}
\newcommand{\Facultad}{\expandafter\MakeUppercase\expandafter{\facultad}}
\Facultad

The problem with your definition is that \uppercase acts on the token list \universidad and doesn't do nothing, because at that level there's no letter to be uppercased; \universidad is expanded only later. With \expandafter we perform the expansion before \MakeUppercase comes into action.

Just as an exercise, here is a macro \Capitalize that takes as argument a control sequence and defines its "uppercase variant"

\makeatletter
\newcommand{\Capitalize}[1]{%
  \edef\@tempa{\expandafter\@gobble\string#1}%
  \edef\@tempb{\expandafter\@car\@tempa\@nil}%
  \edef\@tempa{\expandafter\@cdr\@tempa\@nil}%
  \uppercase\expandafter{\expandafter\def\expandafter\@tempb\expandafter{\@tempb}}%
  \@namedef{\@tempb\@tempa}{\expandafter\MakeUppercase\expandafter{#1}}}
\makeatother

After this magic code you can say

\newcommand\universidad{Universidad de Lugar}
\newcommand{\facultad}{Ingeniería}

\Capitalize{\universidad}
\Capitalize{\facultad}

will define also \Universidad and \Facultad that will print "UNIVERSIDAD DE LUGAR" and "INGENIERÍA".

Note that \MakeUppercase does not go along with hyperref, so in case you use these commands where this package extracts something for building bookmarks you should give it a safe token list; for example

\newcommand{\Facultad}{%
   \texorpdfstring{\expandafter\MakeUppercase\expandafter{\facultad}}%
      {\facultad}}

and, in the automatic defining command, change the line

\@namedef{\@tempb\@tempa}{\expandafter\MakeUppercase\expandafter{#1}}}

into

\@namedef{\@tempb\@tempa}{%
  \texorpdfstring{\expandafter\MakeUppercase\expandafter{#1}}{#1}}}

In case one's using utf8x, the definitions must be preceded by \PrerenderUnicode:

\PrerenderUnicode{Ingeniería}
\newcommand{\facultad}{Ingeniería}
\newcommand{\Facultad}{\expandafter\MakeUppercase\expandafter{\facultad}}

The \Capitalize macro could be

\makeatletter
\newcommand{\Capitalize}[1]{%
  \@ifpackagewith{inputenc}{utf8x}{\PrerenderUnicode{#1}}{}%
  \edef\@tempa{\expandafter\@gobble\string#1}%
  \edef\@tempb{\expandafter\@car\@tempa\@nil}%
  \edef\@tempa{\expandafter\@cdr\@tempa\@nil}%
  \uppercase\expandafter{\expandafter\def\expandafter\@tempb\expandafter{\@tempb}}%
  \@namedef{\@tempb\@tempa}{\expandafter\MakeUppercase\expandafter{#1}}}
\makeatother

so that \PrerenderUnicode is performed automatically when needed.