# How to color parts' title background in TOC with tocloft?

As you can guess from the title, I am using the package tocloft to customize the table of contents of a document, which, by the way, I am compiling with LuaTeX.

The final result that I would like to get can be simplified as follows:

The feature that I am not able to implement on my own is the black background of the part title. Please ignore the other design features of the picture I have posted that may differ from the standard latex TOC (e.g. the missing page number).

Do you know how can I achieve such look? Thanks.

You might want to look into titletoc, it allows to use \colorbox without hacks.

If you really want to keep using tocloft you can use a hack: Capture the title in a \hbox, which you \unhbox later in the colorbox:

\documentclass{book}
\usepackage{tocloft,xcolor}
\newcommand\cftafterparttitle{%
\setlength\fboxsep{1pt}\colorbox{black}{\unhbox0}\egroup%
}
\expandafter\renewcommand
\expandafter\cftpartfont
\expandafter{\cftpartfont
\color{white}%
\setbox0\hbox\bgroup
\aftergroup\cftafterparttitle
}
\begin{document}
\tableofcontents
\part{Teil 1}
\chapter{Kapitel 1}
\section{Abschnitt 1}
\part{Teil 2}
\chapter{Kapitel 2}
\section{Abschnitt 2}
\end{document}


• it works perfectly! thanks. I think I am going to keep using tocloft simply because I have some other customized features which I do not have time to move to titletoc. Would you in general suggest, for the next times, to move to titletoc? – oibaFox Apr 19 '18 at 13:45
• I don't have enough experience with either of them to give a really qualified recommendation, but it looks like titletoc is probably better if you not only just want to change some details. For small changes there is a significant advantage for tocloft through: You can change some separated settings instead of having to change all parameters at one go. – Marcel Krüger Apr 19 '18 at 14:07