10

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?

Mensch
  • 65,388
Aku
  • 11,026
  • Please don't post an answer in the question. This only confuses people who read the question later on. You should add an answer with this solution (and clearly state that it is just a variation of Stefan's answer). – Caramdir Jan 06 '11 at 19:37
  • I did not how to post answer back, so I edited the post. I did not know I can answer my own question. I have restored the original post – Aku Jan 06 '11 at 19:39
  • If you want to, you can still take what you just deleted and post it as an answer. – Hendrik Vogt Jan 07 '11 at 13:54
  • there is an answer form at the bottom of this page. – Caramdir Jan 07 '11 at 16:21

2 Answers2

10

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

Mensch
  • 65,388
Stefan Kottwitz
  • 231,401
  • You have forgotten the subsubsection: \renewcommand*{\l@subsubsection}[2]{% \stdl@subsection{\hspace{1cm}\textcolor{TheColorIWant}{#1}}{\textcolor{TheColorIWant}{#2}}} Here we need the \hspace command because \stdl@subsubsection doesn't exist – Álvaro May 18 '12 at 10:12
  • @Álvaro: I did not forget the subsubsection, as it was asked just for section and subsection, but thanks for the addition. Btw. I converted your answer to a comment, as it's more a comment to the other answer. – Stefan Kottwitz May 18 '12 at 18:04
  • I got the desired colors from the answer above, but the toc is no longer hyperlinked. If I use the hyperref package, it screws with the colors, only page numbers are colored, though the toc is clickable. Any workaround for this? – Damitr May 29 '17 at 16:11
0

I've just solve it!

\usepackage{tocloft}
\renewcommand{\cftsubsubsecfont}{\color{ColorTerciario}}
\renewcommand{\cftsubsubsecaftersnumb}{\hspace{0.5em}}

\makeatletter %colorear la tabla de contenidos, extraído de http://tex.stackexchange.com/questions/7526/toc-section-subsection-coloring/56368#56368
    \let\stdl@section\l@section
    \renewcommand*{\l@section}[2]{%
        \stdl@section{\textcolor{ColorPrincipal}{#1}}{\textcolor{ColorPrincipal}{#2}}}
    \let\stdl@subsection\l@subsection
    \renewcommand*{\l@subsection}[2]{%
        \stdl@subsection{\textcolor{ColorSecundario}{#1}}{\textcolor{ColorSecundario}{#2}}}

The space can be varied with the cftsubsubsecaftersnumb.

Mensch
  • 65,388
Álvaro
  • 1,210