[Tex/LaTex] How to center chapter, section and subsection using memoir

horizontal alignmentmemoirsectioning

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

Best Answer

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