# Center parts in toc with tocloft

I'm trying to create a toc in a large Latex document which uses the KOMA script. In the toc I would like to center the parts and remove all other components (pagenumber, partnumber). I use tocloft (a hard requirement for other reasons).

While I managed to remove the pagenumber, the partnumber is still there and the parts are very much on the right site instead of being centered.

A simplified example is:

\documentclass{scrbook}
\usepackage[ngerman]{babel}

\usepackage{lipsum}

\usepackage{tocloft}
\renewcommand{\cftpartfont}{\hfill\Large\bfseries}
\renewcommand{\cftpartaftersnum}{\hfill}
\addtocontents{toc}{\cftpagenumbersoff{part}}

\begin{document}
\tableofcontents
\part{A}
\section{a}
\lipsum
\part{B}
\section{b}
\lipsum

\end{document}


I have adapted the commands for the centering from an example of the tocloft manual, where


was used to center the title (instead of the parts).

So my question, how can I center the parts and remove the partnumber?

-
add comment

## 1 Answer

Try this:

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage{lipsum}
\usepackage{tocloft}

\addtocontents{toc}{\cftpagenumbersoff{part}}
\renewcommand*{\addparttocentry}[2]{%
\addtocentrydefault{part}{}{\protect\parbox{\textwidth}{\protect\centering#2}}% original #1 in second argument
}

\begin{document}

\tableofcontents

\part{A}
\section{a}
\lipsum
\part{B}
\section{b}
\lipsum

\end{document}


Instead of \hfil I used a \parbox with centered contents, just in case some title would span more than one line. I suppresses the part number in the ToC with a simple redefinition of \addparttocentry.

You mentioned that you need to use tocloft, but if this is not the case, you can use the tocstyle package (designed especifically for the KOMA classes) instead and say something like:

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage{lipsum}
\usepackage{tocstyle}

\usetocstyle{classic}
\settocfeature{pagenumberhook}{}
\renewcommand*{\addparttocentry}[2]{%
\addtocentrydefault{part}{}{\protect\parbox{\textwidth}{\protect\centering#2}}% original #1 in second argument
}

\begin{document}

\tableofcontents

\part{A}
\section{a}
\lipsum
\part{B}
\section{b}
\lipsum

\end{document}


### Warning:

The following solution should be used only for old versions of KOMA-Script (for example, the one in TeX Live2009); for recent versions (those from TeX Live2010 on), please see the code above; I only include this solution here sice the OP is using TeX Live2009 and has no possibility to update:

\documentclass{scrbook}
\usepackage[ngerman]{babel}
\usepackage{lipsum}
\usepackage{tocloft}

\makeatletter
\def\@part[#1]#2{%
\ifnum \c@secnumdepth >-2\relax
\refstepcounter{part}%
\@maybeautodot\thepart%
\addcontentsline{toc}{part}{\protect\makebox[0pt][l]{\protect\parbox{\textwidth}{\protect\centering#1}}}%
\else
\addcontentsline{toc}{part}{#1}%
\fi
\begingroup
\setparsizes{\z@}{\z@}{\z@\@plus 1fil}\par@updaterelative
\raggedpart
\interlinepenalty \@M
\normalfont\sectfont\nobreak
\ifnum \c@secnumdepth >-2\relax
\size@partnumber{\partformat}%
\partheadmidvskip
\fi
\size@part{#2}\strut%
\partmark{#1}\par
\endgroup
\@endpart
}
\makeatother
\addtocontents{toc}{\cftpagenumbersoff{part}}

\begin{document}

\tableofcontents

\part{A}
\section{a}
\lipsum
\part{B}
\section{b}
\lipsum

\end{document}

-
sorry for un-accepting, but it does not work. When I try exactly your example (tocloft), I get "! LaTeX Error: \addparttocentry undefined." –  xubuntix Aug 17 '12 at 8:59
@xubuntix Don't worry (as far as I can see you didn't accept this answer; you accept by ckicking on the green checkmark below the vote count). The error message suggets that you have an updated LaTeX system. Which LaTeX distribution and version do you use? If it's not TeX Live2012 or MikTeX 2.9, you should consider updating your LaTeX system. In the meantime, I'll try to design a solution without \addparttocentry. –  Gonzalo Medina Aug 17 '12 at 13:51
@xubuntix I have a question: I did a test with my code using TeX Live2010, TeX Live2011, and TeX Live2012 and everything worked OK, so if you are getting that error message with my exact code (notice the use of scrbook) then your LaTeX system must be really outdated. Are you sure you used my exact code (using scrbook)? If you use a standard class (book, report) the code won't work, but I chose scrbook because that's what you used in your question. –  Gonzalo Medina Aug 17 '12 at 18:50
I'm using the most up to date package for Ubuntu/Debian: texlive2009-15. Yes, that is very outdated, but I will only install latex via packages, so there is no newer for me available as far as I can see. –  xubuntix Aug 18 '12 at 5:55
@xubuntix I've updated my answer providing a solution that should work with TeX Live2009. Please let me know if things work as expected. –  Gonzalo Medina Aug 19 '12 at 20:48
show 1 more comment