7

I'm bit lost with the content of titletoc.sty. I'd like to access toc entries, meaning chapter title, section title and so on to put them in a colorbox. I think it is somewhere in the \def\ttl@tocentry and I'd like to be able to write something like:

\titlecontents{chapter}[0pc]
{\colorbox{red}{chapter title}}
{}
{}
{}%

EDIT: MWE:

\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{titletoc}
\titlecontents{chapter}%section
[0pc]%left
{}%above
{}%before with label
{}%before without label 
{}%filler and page 
[]%after

\begin{document}
\tableofcontents
\chapter{chapter title}
\end{document}
lockstep
  • 250,273
pluton
  • 16,421

2 Answers2

6

As explained in the manual: "As in \titleformat, the last command can take an argument with the title". So:

\titlecontents{chapter}[0pc]
{}
{\colorbox{red}} % the second argument is left implicit
{}
{}%
Javier Bezos
  • 10,003
  • How can one explicitly access the implicit argument? In order to use tcolorbox instead of colorbox? – rk89 Dec 05 '23 at 16:56
2

I would redefine the internal definition of chapter

\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{titletoc}
\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\colorbox{red}{\protect\numberline{\thechapter}#1}}%
                    \else
                      \addcontentsline{toc}{chapter}{\colorbox{red}{#1}}%
                    \fi
                    \chaptermark{#1}%
                    \addtocontents{lof}{\protect\addvspace{10\p@}}%
                    \addtocontents{lot}{\protect\addvspace{10\p@}}%
                    \if@twocolumn
                      \@topnewpage[\@makechapterhead{#2}]%
                    \else
                      \@makechapterhead{#2}%
                      \@afterheading
                    \fi}
\makeatother
\begin{document}
\tableofcontents
\chapter{chapter title}
\end{document}

Here is another solution:

\documentclass[12pt]{report}
\usepackage{xcolor}
\usepackage{titletoc}
\titlecontents{chapter}%section
[0pc]%left
{}%above
{}%before with label
{}%before without label 
{}%filler and page 
[]%after

\makeatletter
\def\addcontentsline#1#2#3{%
  \addtocontents{#1}{\protect\contentsline{#2}{\colorbox{red}{#3}}{\thepage}}}
\makeatother
\begin{document}
\tableofcontents
\chapter{chapter title}
\section{foo}
\end{document}
Marco Daniel
  • 95,681