[Tex/LaTex] Center parts in toc with tocloft

horizontal alignmentkoma-scripttable of contentstocloft

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

\renewcommand{\cftZtitlefont}{\hfill\Large\bfseries} together with
\renewcommand{\cftafterZtitle}{\hfill} will give a centered Large
bold title.

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

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

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

enter image description here

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}
Related Question