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?

4
  • 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
    Commented Jan 6, 2011 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
    Commented Jan 6, 2011 at 19:39
  • If you want to, you can still take what you just deleted and post it as an answer. Commented Jan 7, 2011 at 13:54
  • there is an answer form at the bottom of this page.
    – Caramdir
    Commented Jan 7, 2011 at 16:21

2 Answers 2

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

3
  • 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
    Commented May 18, 2012 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
    Commented May 18, 2012 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
    Commented May 29, 2017 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.

You must log in to answer this question.

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