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

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

2 Answers 2

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
2
  • This does not work, if you have a manual line break with \newline in the section title. Any ideas?
    – user5950
    Mar 7, 2015 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 7, 2015 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 \\?).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .