[Tex/LaTex] LaTeX package “titling”, titles with hard line breaks, and \MakeUpperCase

line-breakingtitlestitling

I have a document with a title that is spread over several lines using hard line breaks. To configure the title formatting, I use the titling package with code adapted from its documentation:

\documentclass{article}

\usepackage{titling}
\pretitle{\begin{center}\LARGE\sffamily\bfseries}
\posttitle{\par\end{center}\vskip 0.5em}

\title{Higgelty Pigglety Pop! \\ or \\ There Must Be More to Life}

\begin{document}
\maketitle
\end{document}

…and it works perfectly.

I now want the title additionally to be in all caps and therefore change the \pretitle command:

\pretitle{\begin{center}\LARGE\sffamily\bfseries\MakeUppercase}

…and I get the following error message:

! Use of \@icentercr doesn't match its definition.
<argument> \def

l.12 \maketitle

This only happens with a title that includes hard line breaks.

What does this error message mean? How can I fix it?

Best Answer

Split the title at \\ and apply \MakeUppercase to each chunk:

\documentclass{article}

\usepackage{titling}
\pretitle{\begin{center}\LARGE\sffamily\bfseries}
\posttitle{\par\end{center}\vskip 0.5em}
\makeatletter
\renewcommand{\title}[1]{\gdef\@title{\DoUppercase{#1}}}
\DeclareRobustCommand\DoUppercase[1]{\Do@Uppercase#1\\\@nil}
\def\Do@Uppercase#1\\#2\@nil{%
  \MakeUppercase{#1}%
  \ifx\\#2\\%
    \expandafter\@gobble
  \else
    \\\expandafter\@firstofone
  \fi
  {\Do@Uppercase#2\@nil}%
}
\makeatother

\title{Higgelty Pigglety Pop! \\ or \\ There Must Be More to Life}

\begin{document}
\maketitle
\end{document}

This is obtained by redefining \title to store \DoUppercase{<title>} in \@title rather than just the title.

You can't use the vertical spacing optional argument to \\, though.

enter image description here

If you want to support also \\[3pt] or whatever, then it is possible with l3regex.

\documentclass{article}
\usepackage{xparse,l3regex}
\usepackage{titling}
\pretitle{\begin{center}\LARGE\sffamily\bfseries}
\posttitle{\par\end{center}\vskip 0.5em}
\makeatletter
\renewcommand{\title}[1]{\gdef\@title{\DoUppercase{#1}}}
\makeatother

\ExplSyntaxOn
\NewDocumentCommand{\DoUppercase}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \tl_set:Nn \l_tmpb_tl { \\ }
  % Add \MakeUppercase{ at the beginning and } at end
  \regex_replace_once:nnN
   { (.*) }
   { \c{MakeUppercase}\cB\{\1\cE\}} \l_tmpa_tl
  % change \\[...] into }\\[...]\MakeUppercase{
  \regex_replace_all:nnN
   { \u{l_tmpb_tl}\[(.*?)\] }
   { \cE\}\u{l_tmpb_tl}\[\1\]\c{MakeUppercase}\cB\{ }
   \l_tmpa_tl
  % change \\ into }\\\MakeUppercase{
  \regex_replace_all:nnN
   { \u{l_tmpb_tl}([^\[]) }
   { \cE\}\u{l_tmpb_tl}\c{MakeUppercase}\cB\{\1 }
   \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\title{Higgelty Pigglety Pop! \\[3pt] or \\ There Must Be More to Life}

\begin{document}
\maketitle
\end{document}