9

I'm trying to capitalize the first, and only the first letter of line which I have defined as \mytext. I've used {\MakeUppercase put text here?}... until now. But now I have it as a variable

\documentclass{article}

\begin{document}
  \def \mytext {hi how are you?}       

{\MakeUppercase hi how are you?} \par        % => Hi how are you?
{\MakeUppercase \mytext}                     % => HI HOW ARE YOU?
\end{document}

How can I make the first letter of the defined variable capitalized? Thanks in advance!

  • 5
    Possible duplicate of Command to uppercase the first letter of each word in a sentence – Moriambar Apr 4 '17 at 18:13
  • @Moriambar, I had already seen that question and it's answer and I didn't quite get what would answer my question. – Bjartmar Apr 4 '17 at 18:51
  • @Moriambar although that question does cover it, its complexified by the addition of the indexing requirement. – Paul Stanley Apr 4 '17 at 19:51
  • @PaulStanley I understand. Sorry! – Moriambar Apr 4 '17 at 20:08
  • 1
    See David's answer, below. Here is what happened: {\MakeUppercase x} applies uppercase to whatever x is. If x is \mytext it applies uppercase to the text as a unit, then writes it out. But with the \expandafter used, \mytext is first written out (virtually) THEN uppercase is applied, so it catches just the first letter of the text. – user103221 Apr 4 '17 at 22:27
13
  \expandafter\MakeUppercase \mytext
3

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}

Hi how are you? *Hi* how are you?

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}

“Hi how are you?”

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}

Ĥi how are you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.