# How to center chapter, section and subsection using memoir?

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}


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}

\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}


• 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 '14 at 4:42

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.

\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}

\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

• This does not work, if you have a manual line break with \newline in the section title. Any ideas? – user5950 Mar 7 '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 7 '15 at 17:20

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

\sethangfrom{\noindent #1}