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}
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
{}
{}%
  • Ok thanks, I'll read the manual again and try to see how I can handle your other response as well (even though I do not feel it is going to be straightforward). – pluton Jun 6 '11 at 11:51
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}
  • is titletoc needed in your example? I once thought it would be more efficient to redefine the tableofcontents than using titletoc. – pluton Jun 5 '11 at 17:47
  • @pluton: in the example titletoc is useless. – Marco Daniel Jun 5 '11 at 18:15
  • I just tried to replace \colorbox by a Tikz picture but it does not work. Seems impossible. – pluton Jun 5 '11 at 18:19

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.