11

I am trying to centre chapter, section and subsection using memoir, with the following code:

\renewcommand{\chapnamefont}{\centering\normalfont} 
\renewcommand{\chapnumfont}{\centering\normalfont} 
\renewcommand\chaptitlefont{\centering\LARGE\normalfont}
\renewcommand\secheadstyle{\centering\Large\normalfont\noindent}

This works, but when the section title is longer than one line, the second one isn't centred anymore to the middle of the page, but only to the middle of the first line minus the space that is occupied by the section numbers. How to centre all lines of the section titles to the middle of the page?

Here is a working example:

\documentclass[a4paper,10pt,twoside]{memoir}

\usepackage{polyglossia} 
\setmainlanguage[spelling=old,babelshorthands=true,script=latin]{german}
\usepackage{blindtext}
\usepackage{fontspec}

\renewcommand{\chapnamefont}{\centering\normalfont} 
\renewcommand{\chapnumfont}{\centering\normalfont} 
\renewcommand\chaptitlefont{\centering\LARGE\normalfont}
\renewcommand\secheadstyle{\centering\Large\normalfont\noindent}

\begin{document}
\tableofcontents*
\chapter{Test, nothing else – Don't waste your time reading this! It has to have two lines}
\section{Don't waste your time reading this! It has to have two lines. It has to have two lines}
\Blindtext
\footnote{\blindtext}
\blindtext
\section{Test124}
\Blindtext
\chapter{Test2}
\Blindtext \Blindtext \Blindtext
\end{document}

enter image description here

user5950
  • 1,456
  • Are you open for section numbers to be typeset in the margin, then \hangsecnum is a possible solution to your centering problem? –  Nov 29 '14 at 04:42

2 Answers2

7

Drop the \@hangfrom style of the section using the following patch:

\usepackage{etoolbox}
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\M@sect}{\@hangfrom}{}{}{}
\makeatother

\@hangfrom takes the width of the section number into play and adjusts the layout accordingly. As such, it allows for sectional titles being displayed in a hanging style. However, for a centered view on things, this is of course not really ideal.

enter image description here

\documentclass{memoir}

\usepackage{blindtext,etoolbox}
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\M@sect}{\@hangfrom}{}{}{}
\makeatother

\renewcommand{\chapnamefont}{\centering\normalfont} 
\renewcommand{\chapnumfont}{\centering\normalfont} 
\renewcommand\chaptitlefont{\centering\LARGE\normalfont}
\renewcommand\secheadstyle{\centering\Large\normalfont\noindent}

\begin{document}
\chapter{Test, nothing else – Don't wast you time reading this! It has to have two lines}
\section{Don't wast you time reading this! It has to have two lines. It has to have two lines}
\Blindtext
\end{document}

If you're loading hyperref, the patch is to the macro \H@old@sectm@m instead:

\usepackage{hyperref,etoolbox}
\makeatletter
\patchcmd{\H@old@sectm@m}{\@hangfrom}{}{}{}
\makeatother
Werner
  • 603,163
  • This does not work, if you have a manual line break with \newline in the section title. Any ideas? – user5950 Mar 07 '15 at 16:21
  • @user5950: The problem may be that the line break doesn't work as-is in a he ToC. Why use that anyway? – Werner Mar 07 '15 at 17:20
2

I found a more simple way to solve the problem with memoir. Just add this command to your tex file:

\sethangfrom{\noindent #1}

For more information see memoir documentation, page 95.

If you have to use manual line breaks in the tiltles, note that \\ will be redefined by \centering while \newline won't. For that reason using \newline with \centering will give undesired results (see What is the difference between \newline and \\?).

user5950
  • 1,456