3

How do I get the below code snippet output "Myword"?

\documentclass{article}

\newcommand{\mymacro}{myword}

\begin{document}

\mymacro

\end{document}
1
  • Are you using pdflatex? If so, don't use \uppercase under any circumstance, unless you know precisely why, which is not the case for the present application.
    – egreg
    Commented Jun 16, 2023 at 8:31

3 Answers 3

6

Use \uppercase primitive and correct order of expansion

\def\myword{myword}
\def\capitalizefirst#1{\uppercase{#1}}

\expandafter\capitalizefirst\myword.
4
  • 2
    Don't use \uppercase in LaTeX! Your code will fail, with PDFLaTeX and non-us-ascii letters, e.g. \def\myword{özi} will still show a lowercase ö instead of Ö.
    – cabohah
    Commented Jun 16, 2023 at 5:48
  • 2
    This is problem of LaTeX with old 8bit TeX engines, this isn't problem of \uppercase primitive itself. If you are using Unicode TeX engines (or csplain with old pdftex engine) then there is no problem with \uppercase. We can't be limited by old mistakes in old LaTeX today.
    – wipet
    Commented Jun 16, 2023 at 7:12
  • 1
    This is not a mistake of old LaTeX, it is a limitation of the engine. Because of that limitation LaTeX provides \MakeUppercase for decades. And we are not limited, because up-to-date LaTeX provides a solution even for this special case. Just see my answer, which uses that solution. With plainTeX using LuaTeX or XeTeX, your suggestion may be fine. Because of this, I've not down-voted, but because you've not explained this limitation in your answer, I've added a comment. I think it is fair, to at least tell users the limitations of an answer.
    – cabohah
    Commented Jun 16, 2023 at 8:14
  • 2
    The \uppercase{á} works in csplain + pdftex, so it is possible with 8 bit tex engine but not in LaTeX + pdftex. Today, it is no reason to use 8bit TeX engine.
    – wipet
    Commented Jun 16, 2023 at 10:06
5

With an up-to-date LaTeX kernel you can use \MakeTitlecase:

\documentclass{article}

\newcommand{\mymacro}{myword}

\begin{document}

\MakeTitlecase{\mymacro}

\end{document}

Myword

2
  • Doesn't seem to work with pdflatex 3.14
    – sherlock
    Commented Jun 16, 2023 at 15:03
  • @sherlock If you get an error about unkown command sequence \MakeTitlecase, your LaTeX kernel is too old. You need at least LaTeX 2022-11-01. The current LaTeX release 2023-06-01 also works. I've tested with pdflatex, lualatex and xelatex.
    – cabohah
    Commented Jun 16, 2023 at 18:35
2

You can use mfirstuc's \makefirstuc:

enter image description here

\documentclass{article}

\usepackage{mfirstuc}

\newcommand{\mymacro}{\makefirstuc{myword}}

\begin{document}

\mymacro

\end{document}

mfirstuc also provides \emakefirstuc{<stuff>} which expands <stuff> first, before capitalization. Your use would be \emakefirstuc{\mymacro}.

2
  • I don't want to modify the macro itself
    – sherlock
    Commented Jun 16, 2023 at 5:25
  • 2
    @sherlock You can use \emakefirstuc{\mymacro} without changing \mymacro. See the manual of mfirstuc for more information.
    – cabohah
    Commented Jun 16, 2023 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .