2

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.

enter image description here

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 !

4
  • 3
    Can you please make a compilable minimal working example (MWE)? Jun 10, 2017 at 20:47
  • Of course. I'm sorry, I'm new to Latex and this website. Jun 11, 2017 at 6:40
  • 1
    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, 2017 at 6:45
  • I tried to, but dots and page numbers are in black in the toc. Jun 11, 2017 at 11:48

1 Answer 1

2

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
\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
     \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{%
\addcontentsline{toc}{section}{#2}}

\makeatother

\newcommand*{\setsectoccolor}[1]{\addtocontents{toc}{\gdef\string\sectoccolor{\protect#1}}}  

\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}

enter image description here

(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)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .