Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
Works like a charm, thanks. –  user1362373 Nov 29 '11 at 15:27
add comment

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.

share|improve this answer
    
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
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. –  Gonzalo Medina 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. –  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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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