Capitalize first letter of a defined variable
\expandafter\MakeUppercase \mytext
Here's another approach:
\documentclass{article}
\usepackage{mfirstuc}
\begin{document}
\def \mytext {hi how are you?}
\xmakefirstuc{\mytext}
\end{document}
This works much like David's answer, but can also handle awkward cases where the text includes text-block formatting commands (where the first argument is the text that needs formatting). For example:
\documentclass{article}
\usepackage{mfirstuc}
\begin{document}
\def \mytext {hi how are you?}
\xmakefirstuc{\mytext}
\def \mytext {\emph{hi} how are you?}
\xmakefirstuc{\mytext}
\end{document}
In the second case above, it's effectively doing:
\emph{\MakeUppercase hi} how are you?
A simple \expandafter\MakeUppercase\mytext
approach causes:
\MakeUppercase\emph{hi} how are you?
which doesn't work as it's attempting \MakeUppercase{\emph}
.
The commands provided by mfirstuc
don't work with declarations (such as \em
or \itshape
) as the package is designed for semantic markup not free-form formatting. You can use semantic markup to deal with quoted or parenthetical material. For example:
\documentclass{article}
\usepackage{mfirstuc}
\begin{document}
\newcommand*{\qt}[1]{``#1''}
\def \mytext {\qt{hi how are you?}}
\xmakefirstuc{\mytext}
\end{document}
Another point to consider is whether or not you intend using UTF-8 characters with inputenc
. This isn't a problem with XeLaTeX or LuaLaTeX, but with LaTeX the UTF-8 character is treated as two octets. For example:
\def \mytext {ĥi how are you?}
\expandafter\MakeUppercase\mytext
This causes the error:
! Argument of \UTFviii@two@octets has an extra }.
because \MakeUppercase
has only grabbed the first octet. The same error occurs with \xmakefirstuc
unless you load datatool-base
(at least v2.24) after inputenc
and before mfirstuc
:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{datatool-base}
\usepackage{mfirstuc}
\begin{document}
\def \mytext {ĥi how are you?}
\xmakefirstuc{\mytext}
\end{document}