2

I have searched for a whole day now and really could not find anything about it. I want to change the style of the TOC in my document. Specifically, I want to have a background color to the section numbers in it, much like this:

I want this only on the top level sections, so the sub- and subsubsection still appear in their normal font and all.

Now I've tried around with tocloft and feel like \thesec is somehow what I want. I can't figure out how to add a colourbox around it though, or how to use it in general. I have used Latex for years now but never really used any scripts or so. This is what I have so far:

\documentclass[10pt,a4paper,twoside,titlepage]{article}
\usepackage{tocloft}
\usepackage[T1]{fontenc}

\usepackage{color}
\definecolor{redl}{RGB}{255,52,52}
\definecolor{redd}{RGB}{149,28,23}

\makeatletter

\let\stdl@section\l@section
\renewcommand*{\l@section}[2]{%
\stdl@section{\textsc{\textcolor{redd}{#1}}}{\textcolor{redd}{#2}}}
%\let\thesecc\thesec
%\renewcommand*{\thesec}{\textcolor{redl}{\thesecc}}
\makeatother

\begin{document}
\tableofcontents

\section{Foo Test}
    \subsection{bar}
    \subsection{bar}
\end{document}

sets the top level section entries in redd (dark red, defined by me) and in small capitals. The two commented lines are a first try of using \thesec, that failed.

I would appreciate if someone had an idea. Please let me know if you need a MWE for this.

Best, Hannes

Edit1: Added the MWE

Edit2: Replaced \thesection by \thesec: Noticed that the actual section titles in the document were changed as well. I want the colourboxes for the TOC and for the headings to be defined seperately, as the sizes are obviously different. Thus my question about the TOC entries, as that seems more difficult.

1 Answers1

1

I do not know how this can be done using package tocloft. So here is only a suggestion using tocbasic

\documentclass[10pt,a4paper,twoside,titlepage]{article}
\usepackage[T1]{fontenc}

\usepackage{xcolor}% to use color lightgray (loads also color)
\definecolor{redl}{RGB}{255,52,52}
\definecolor{redd}{RGB}{149,28,23}

\usepackage{tocbasic}[2016/05/10]% needs version 3.20 or newer
\newcommand\secnumbox[1]
  {\colorbox{lightgray}{\makebox[1.5ex]{#1}}}
\newcommand\secentryformat[1]
  {\scshape\bfseries\textcolor{redd}{#1}}
\DeclareTOCStyleEntry[
  entrynumberformat=\secnumbox,
  entryformat=\secentryformat,
  pagenumberformat=\secentryformat
]{tocline}{section}

\begin{document}
\tableofcontents

\section{Foo Test}
    \subsection{bar}
    \subsection{bar}
\end{document}

results in

enter image description here

esdd
  • 85,675
  • Thank you for your answer. I am getting an undefined control sequence \DeclareTOCStyleEntry when running it? – hannesra Jun 12 '17 at 10:20
  • Package tocbasic is part of the KOMA-Script bundle. You need at least version 3.20 from May 2016. Current version on CTAN etc. is 3.23 – esdd Jun 12 '17 at 12:06