0

How can I modify the part style, so that instead of a rule I get a gray background? I'm using tocloft package.

\addtocontents{toc}{\protect\mbox{}\protect\hrulefill\par}

\documentclass{book}
\usepackage{tocloft}
\begin{document}
\tableofcontents

\part{Part 1}
\addtocontents{toc}{\protect\mbox{}\protect\hrulefill\par}
\chapter{Lorem ipsum}
\section{Dolor sit amet}

\end{document}
3

My solution is to use tcolorbox package with redefining a standard class command \l@part[2]

\documentclass{book}
\usepackage{tocloft}
\usepackage{tcolorbox}

\makeatletter
\renewcommand*\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \begin{tcolorbox}[colback=black!10, right=0.5ex, left=0cm, sharpish corners, boxrule=0mm, width=1.01\textwidth]
      {\leavevmode
       \large \bfseries \vbox{\sffamily\bfseries#1}%
      \hb@xt@.5ex{\hss #2}
       }
       \end{tcolorbox}
       \vspace*{1ex}
       \nobreak
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}
 \makeatother
\begin{document}
\tableofcontents

\part{Part 1}
\chapter{Lorem ipsum}
\section{Dolor sit amet}

\end{document}

enter image description here

  • I get a white frame around the title? Unfortunately I cannot give an example. – Xeo Jan 21 '15 at 12:04
  • @Xeo Try to redefine colback to colback=black!30 or something desirable – sergiokapone Jan 21 '15 at 12:48
  • Thank you! I solve the issue, it was because of the hyperlinks!!! – Xeo Jan 21 '15 at 13:29

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.