I'm trying to customise my table of contents. I would like to set a colour for each of my sections, as you can see in the image below. Purple for activity, teal for lessons, and blue for exercices.

I'm just adding a counter to the definition of l@section to do that :

\documentclass[11pt,a4paper,french]{book}
\usepackage{xstring}
\usepackage{titlesec}
\usepackage{tikz}

\newcounter{colorCounter}

\makeatletter

\renewcommand*\l@section[2]{%
\refstepcounter{colorCounter}
\ifnum\c@tocdepth>\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries \large
\hskip -\leftskip
\IfStrEq{\thecolorCounter}{1}{
\IfStrEq{\thecolorCounter}{2}{
\IfStrEq{\thecolorCounter}{3}{
\begin{tikzpicture}
\end{tikzpicture}%}
\nobreak\
\leaders\hbox{$\m@th \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
\penalty\@highpenalty
\ifnum\value{colorCounter}=3 \setcounter{colorCounter}{0}\fi
\endgroup
\medskip
\fi}

\def\section{\secdef\@section\@ssection}

\def\@section[#1]#2{%

\makeatother

\begin{document}
\tableofcontents
\newpage
\chapter{Probability}
\section{Activity}
bla
\section{Cours}
bla
\section{Exercices}
bla
\chapter{Tableur}
\section{Exercices}
bla
\end{document}


Except that, If I don't have an "Activity" section or a "lesson" section, it's not the good colour any more !

• Can you please make a compilable minimal working example (MWE)? – user36296 Jun 10 '17 at 20:47
• Of course. I'm sorry, I'm new to Latex and this website. – Yohann29100 Jun 11 '17 at 6:40
• Wouldn't it be better to use a new command \lesson that sets the lesson title as well as the toc entry? Avoids the akward if construction. – Johannes_B Jun 11 '17 at 6:45
• I tried to, but dots and page numbers are in black in the toc. – Yohann29100 Jun 11 '17 at 11:48

Based on Change images in custom ToC

This basically smuggles the colour changing instructions into the toc:

\documentclass[11pt,a4paper]{book}
\usepackage{xstring}
\usepackage{titlesec}
\usepackage{tikz}

\makeatletter

\renewcommand*\l@section[2]{%
\ifnum\c@tocdepth>\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries \large
\hskip -\leftskip
\sectoccolor\rule{1.7ex}{1.7ex}~#1
\nobreak\
\leaders\hbox{$\m@th \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
\sectoccolor#2}\par
\penalty\@highpenalty
\endgroup
\medskip
\fi}

\def\section{\secdef\@section\@ssection}

\def\@section[#1]#2{%

\makeatother

\setsectoccolor{\color{black}}

\newcommand{\Activity}{%
\setsectoccolor{\color{purple!75}}%
\section{Activity}%
\setsectoccolor{\color{black}}%
}

\newcommand{\Course}{%
\setsectoccolor{\color{teal!75}}%
\section{Course}%
\setsectoccolor{\color{black}}%
}

\newcommand{\Exercices}{%
\setsectoccolor{\color{blue!75}}%
\section{Exercices}%
\setsectoccolor{\color{black}}%
}

\begin{document}
\tableofcontents
\newpage
\chapter{Probability}

\Activity
bla
\Course
bla
\Exercices
bla
\chapter{Tableur}
\Exercices
bla

\setsectoccolor{\color{orange!75}}%
\section{some other section}
\setsectoccolor{\color{black}}%

\end{document}


(maybe it would be enough to smuggle the colour name into the toc, but as the above seems to work, I did not investigate further)