15

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!

Bernard
  • 271,350
Bjartmar
  • 1,096

2 Answers2

21
  \expandafter\MakeUppercase \mytext
David Carlisle
  • 757,742
5

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?

Nicola Talbot
  • 41,153
  • Is there a way to protect a certain word and keep it unchanged? For example, suppose you want to write the sentence: "Welcome to the USA", whether it is written in all caps (WELCOME TO THE USA) or not (welcome to the USA). I thought about using \makefirstuc{\MakeLowercase{ \mytext }} but it does not do the job. Any tip? It would be nice if using braces around the word, it could preserve as it is, for example using welcome to the {USA} . – LEo Dec 11 '20 at 19:04