[Tex/LaTex] TOC Section & Subsection coloring

colortable of contents

Is it possible to create a TOC with section headings in one color and subsection headings in different color.

\tableofcontents
\setcounter{tocdepth}{2}

I am using above lines to create TOC, do I need to use any special package?

Best Answer

You could use the tocloft package for customizing the table of contents.

Without an additional package, you could modify the class macros which are responsible for the table of contents entries, such as \l@section and \l@subsection.

Here is an example for the article class:

\documentclass[11pt]{article}
\usepackage[svgnames]{xcolor}
\makeatletter
\let\stdl@section\l@section
\renewcommand*{\l@section}[2]{%
  \stdl@section{\textcolor{blue}{#1}}{\textcolor{blue}{#2}}}
\let\stdl@subsection\l@subsection
\renewcommand*{\l@subsection}[2]{%
  \stdl@subsection{\textcolor{DarkGreen}{#1}}{\textcolor{DarkGreen}{#2}}}
\makeatother
\setcounter{tocdepth}{2}
\begin{document}
\tableofcontents
\section{Section One}
\subsection{Subsection One}
\subsection{Subsection Two}
\section{Section Two}
\subsection{Subsection One}
\subsection{Subsection Two}
\end{document}

alt text

Related Question