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
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \bfseries \large
\advance\leftskip\@tempdima
\hskip -\leftskip
\IfStrEq{\thecolorCounter}{1}{
\def\@linkcolor{purple!75}}{}
\IfStrEq{\thecolorCounter}{2}{
\def\@linkcolor{teal!75}}{}
\IfStrEq{\thecolorCounter}{3}{
\def\@linkcolor{blue!75}}{}
\begin{tikzpicture}
\node[fill=\@linkcolor,text=white] {};
\end{tikzpicture}%}
\color{\@linkcolor}#1
\nobreak\
\leaders\hbox{$\m@th
\mkern \@dotsep mu\hbox{.}\mkern \@dotsep
mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
\color{\@linkcolor}#2}\par
\penalty\@highpenalty
\ifnum\value{colorCounter}=3 \setcounter{colorCounter}{0}\fi
\endgroup
\medskip
\fi}
\def\section{\secdef\@section\@ssection}
\def\@section[#1]#2{%
\addcontentsline{toc}{section}{#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 !


\lessonthat sets the lesson title as well as the toc entry? Avoids the akward if construction. – Johannes_B Jun 11 '17 at 06:45