First letter upper case and other letters in lower case

\documentclass{article}
\newcommand\capmystring[1]{\capmystringaux#1\relax}
\def\capmystringaux#1#2\relax{\uppercase{#1}\lowercase{#2}}
\begin{document}
eXample String

\capmystring{eXample String}
\end{document}

enter image description here

In this case, \capmystring sends the argument to an auxiliary routine with an attached \relax token appended at the end. The auxiliary routine reads two arguments up to the \relax (one would hope and assume that the argument itself contains no \relax tokens). The way TeX absorbs arguments, the argument #1 will be a single token, whereas #2 will be all the remaining tokens up to (but not including) the originally supplied \relax. At that point it is as simple as applying \uppercase to the first token stored in #1, and \lowercase to the remaining tokens, all of which are stored in #2.


The expl3 language can do this using Unicode data:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand \firstcap { m } { \text_titlecase:n {#1} }
\ExplSyntaxOff
\begin{document}
eXample String

\firstcap{eXample String}
\end{document}

(assuming a new enough LaTeX2e format; for older cases, add \usepackage[utf8]{inputenc} with pdfTeX.)

We might extend this approach to allow for language-specific case mappings

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewExpandableDocumentCommand \firstcap { O{} m }
  { \text_titlecase:nn {#1} {#2} }
\ExplSyntaxOff
\begin{document}
\firstcap{IJSSELMEER}     % Ijsselmeer
\firstcap[nl]{IJSSELMEER} % IJsselmeer
\end{document}

Another solution using the stringstrings package:

\documentclass{article}

\usepackage{stringstrings}
\newcommand{\firstcap}[1]{\caselower[e]{#1}\capitalize{\thestring}}

\begin{document}
eXample String

\firstcap{eXample String}
\end{document}

Explanation: \caselower makes the whole string lowercase, the option [e] saves the result in \thestring such that it can be further processed, finally \capitalize does what it says and outputs the result.

Tags:

Text

Control