Centering part entries in toc using memoir

I'm using memoir for a large document and want to center the toc entries corresponding to parts (page entries are turned off). I read the manual (specifically pp. 161-166) and while I have no problem adjusting the positions of the part number or title separately, I can't figure out how to center the part number and the title together as a whole. The MWE that demonstrates this is as follows:

\documentclass{memoir}

\cftpagenumbersoff{part}

\begin{document}
\tableofcontents*
\part{Part A}
\chapter{Chapter a}
\end{document}


To be more specific, I would like "I Part A" to be horizontally centered. If there were a second part called "B", I would like that to be centered like "II Part B".

Hmm, that can be done with much shorter code:

\makeatletter
\renewcommand\partnumberline[1]{\hfil\hspace\@tocrmarg #1~}
\makeatother


(not entirely sure why though, it's late ...)

• I like that solution but it does not work, where the title of part is longer then one line and you have to have a line break. Then the second line is not centred. Any ideas? – user5950 Oct 21 '14 at 12:44

You can redefine the \@part command as defined in memoir.cls; this command in in charge of producing the part entries in the ToC; the redefinition basically consists in introducing two \hfils to center the entries:

\documentclass{memoir}

\cftpagenumbersoff{part}
\makeatletter
\long\def\@part[#1]#2{%
\M@gettitle{#1}%
\phantomsection
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\mempartinfo{\thepart}{#1}{#2}%
\else
\mempartinfo{}{#1}{#2}%
\fi
\partmark{#1}%
{\centering
\interlinepenalty \@M
\parskip\z@
\normalfont
\ifnum \c@secnumdepth >-2\relax
\midpartskip
\fi
\printparttitle{#2}\par}%
\@endpart}
\makeatother

\begin{document}

\tableofcontents*
\part{Part A}
\chapter{Chapter a}
\part{Part B}
\chapter{Chapter b}

\end{document}


The above code will center the entries with respect to the text width minus the length \@tocrmarg (default value of 2.55em) reserved to typeset the page numbers for other sectional units using some extra space; you can change this, and center the entries with respect to the full text width by adding \hspace*{\@tocrmarg} just before the first \hfils in my example code above.

• Thanks, but the entries do not appear to be centered with respect to the text block. They appear to be slightly to the left of center. – user1362373 Nov 28 '11 at 20:42
• @Nut Yes. That's because they are centered having into account the length \@tocrmarg (dafault value of 2.55em) reserved to typeset the page numbers for other sectional units using some extra space; you can change this by adding \hspace*{2.55em} just before the first \hfils in my example code above. – Gonzalo Medina Nov 28 '11 at 21:15
• Better still, insert the command \hspace*{\@tocrmarg} (instead of \hspace*{2.55em}): that way, one needn't even look up the value of this parameter. :-) – Mico Nov 28 '11 at 22:36
• @Mico: right. Added. – Gonzalo Medina Nov 28 '11 at 22:47
• Thanks to Gonzalo and Mico. I selected daleif's answer since it's short and gives proper centering. – user1362373 Nov 29 '11 at 15:28

Using memoir internal macros, I centered parts in ToC with the following code in my preamble :

\renewcommand*{\cftpartfont}{\hspace{\@pnumwidth}\hfil}
\renewcommand*{\cftpartpresnum}{\hspace{\@pnumwidth}}
\renewcommand*{\cftpartafterpnum}{\hfil}


It uses \cftpartfont and \cftpartafterpnum as hooks and correct for \@pnumwidth (width reserved for page numbers) shift.

I also use this to get rid of parts' page numbers :

\cftpagenumbersoff{part}