9

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".

5

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 ...)

1
  • 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
5

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}%
    \addcontentsline{toc}{part}{\hfil\thepart~#1\hfil}%
    \mempartinfo{\thepart}{#1}{#2}%
  \else
    \addcontentsline{toc}{part}{\hfil#1\hfil}%
    \mempartinfo{}{#1}{#2}%
  \fi
  \partmark{#1}%
  {\centering
   \interlinepenalty \@M
   \parskip\z@
   \normalfont
   \ifnum \c@secnumdepth >-2\relax
     \printpartname \partnamenum \printpartnum
     \midpartskip
   \fi
   \printparttitle{#2}\par}%
  \@endpart}
\makeatother

\begin{document}

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

\end{document}

enter image description here

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.

6
  • 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. Nov 28 '11 at 20:42
  • 1
    @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. Nov 28 '11 at 21:15
  • 1
    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. Nov 28 '11 at 22:47
  • Thanks to Gonzalo and Mico. I selected daleif's answer since it's short and gives proper centering. Nov 29 '11 at 15:28
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.