Combining \chapter command and \newcommand

I attach my MWE:

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\newcommand{\upper}[1]{\expandafter\MakeUppercase\expandafter{#1}}

\begin{document}

\chapter{\upper{Foo bar}}

\end{document}


so what I want to do is to get chapter with capitalized chapter title. However if I use \chapter{\upper{Foo\\ bar}}, my MWE does not work. Why and how can I repair that?

• You should specify the uppercasing as part of the chapter formatting, you shouldn't have formatting within the \chapter argument. – David Carlisle Jan 7 '16 at 21:03
• So you want a line-break in your title? The problem stems from the fact that the title makes its way into the ToC, which is dealt with by writing content to a file... something that is fragile. Your line-breaking technique \\ causes problems here. You can use \chapter{\upper{Foo \protect\\ bar}} but that would issue a line-break in the ToC also. Instead just use \chapter[Foo bar]{\upper{Foo \protect\\ bar}} if you must. – Werner Jan 7 '16 at 21:06
• in your example code the \expandafter are doing nothing as once #1 is inserted it is \expandafter\MakeUppercase\expandafter{Foo bar} so the \expandafter are applied to the token F which is not expandable. – David Carlisle Jan 7 '16 at 21:21

You should avoid putting formatting in the argument (not least because it will affect tables of contents and "named" cross referencing.)

there are several ways of specifying the heading format for example the titlesec package:

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}

\titleformat{\chapter}[block]
{\normalfont\huge\bfseries}{\thechapter}{1em}{\Huge\MakeUppercase}

\begin{document}

\tableofcontents

\chapter{Foo\\bar}

\end{document}


The standard layout of the book class is obtained with

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}
{\MakeUppercase{\chaptertitlename}\ \thechapter}
{1em}
{\Huge\MakeUppercase}

\begin{document}

\tableofcontents

\chapter{Foo\\bar}

\end{document}


• Not sure about the first \MakeUppercase – egreg Jan 7 '16 at 21:20
• @egreg as far as I can see it does nothing too bad, it's first time I've used titlesec I just googled for an example and stuck \MakeUppercase in any argument that looked like a font spec:-) (I'll take it out though) – David Carlisle Jan 7 '16 at 21:24
• Well, remove it; and use [display] instead of [block]; also it should be \chaptertitlename\ \thechapter if one wants to emulate the standard format. – egreg Jan 7 '16 at 21:26

Traditional redefinition of \@makechapterhead and usage of an \if.... - switch. (One could use \xpatchcmd from xpatch as well)

\documentclass[12pt, a4paper]{book}
\usepackage[utf8]{inputenc}

\newif\ifuppercasechaptertitle
\uppercasechaptertitletrue

\makeatletter
\vspace*{50\p@}%
{\parindent \z@ \raggedright \normalfont
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\huge\bfseries \@chapapp\space \thechapter
\par\nobreak
\vskip 20\p@
\fi
\fi
\interlinepenalty\@M
\Huge \bfseries \ifuppercasechaptertitle\MakeUppercase{#1}\else#1\fi\par\nobreak
\vskip 40\p@
}}
\makeatother

\begin{document}

\chapter{Foo}
\uppercasechaptertitlefalse
\chapter{Foo bar}

\end{document}