Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing the table of contents with two colors in Latex

In my document I want to use two colours in table of contents to differentiate the work between two authors. For instance,

  • The chapters, sections and subsections coloured in blue are written by author X and in red colour are written by author Y.

In some chapters, sections and subsections are written by both of the authors. For example in chapter A(Blue),

  • Section 1(Blue) is written by X.
  • Subsection 1.1(Red) and section 2(Red) are written by Y.

Finally, these colours must be only changed in table of contents but not in the content of the document.

How can I customize like that? Would anybody be able to help with something like that?

Thanks in advance.

like image 882
Dheeraj Avatar asked Oct 14 '25 03:10

Dheeraj


1 Answers

With tocloft you have control over the formatting of every entry type within the ToC. For chapters there's \cftchapfont, for sections there's \cftsecfont and subsections have \cftsubsecfont.

Below provide \authoredby{<name>} which inserts an entry into the ToC for changing the colour. Additionally, author colours can be defined using \defineauthorcolor{<name>}{<colour>}.

enter image description here

\documentclass{book}
\usepackage{tocloft,xcolor}

\newcommand{\defineauthorcolor}[2]{%
  \colorlet{author#1}{#2}% Create an author colour
  \expandafter\def\csname authoredby#1\endcsname{% Create author colour settings
    \renewcommand{\cftchapfont}{\bfseries\color{author#1}}% Chapter colour
    \renewcommand{\cftsecfont}{\color{author#1}}% Section colour
    \renewcommand{\cftsubsecfont}{\color{author#1}}}% Subsection colour
}
\makeatletter
\newcommand{\authoredby}[1]{\addtocontents{toc}{\protect\@nameuse{authoredby#1}}}%
\makeatother

\defineauthorcolor{A}{red}% Author A will be coloured red
\defineauthorcolor{B}{blue}% Author B will be coloured blue

\begin{document}

\tableofcontents

\authoredby{A}
\chapter{A chapter}
\authoredby{B}
\section{A section}
\subsection{A subsection}
\authoredby{A}
\section{Another section}
\subsection{Another subsection}
\authoredby{B}
\subsection{Yet another subsection}

\chapter{Another chapter}
\section{First section}
\section{Second section}
\section{Last section}

\authoredby{A}
\chapter{Last chapter}

\end{document}

The .toc for the above minimal example looks like this:

\@nameuse {authoredbyA}
\contentsline {chapter}{\numberline {1}A chapter}{3}
\@nameuse {authoredbyB}
\contentsline {section}{\numberline {1.1}A section}{3}
\contentsline {subsection}{\numberline {1.1.1}A subsection}{3}
\@nameuse {authoredbyA}
\contentsline {section}{\numberline {1.2}Another section}{3}
\contentsline {subsection}{\numberline {1.2.1}Another subsection}{3}
\@nameuse {authoredbyB}
\contentsline {subsection}{\numberline {1.2.2}Yet another subsection}{3}
\contentsline {chapter}{\numberline {2}Another chapter}{5}
\contentsline {section}{\numberline {2.1}First section}{5}
\contentsline {section}{\numberline {2.2}Second section}{5}
\contentsline {section}{\numberline {2.3}Last section}{5}
\@nameuse {authoredbyA}
\contentsline {chapter}{\numberline {3}Last chapter}{7}

Each use of \authoredby{<name>} inserts the \@nameuse{authoredby<name>} into the ToC to switch the colour accordingly. Note that this solution works for any number of authors (not just limited to two).

This solution should work with the book, report and article document classes.

like image 67
Werner Avatar answered Oct 16 '25 23:10

Werner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!