1

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?

  • 1
    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
  • 1
    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
3

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}

enter image description here

  • 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
2

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
\def\@makechapterhead#1{%
  \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.