Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 6 '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 6 '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 7 '11 at 13:54
    
there is an answer form at the bottom of this page. –  Caramdir Jan 7 '11 at 16:21
add comment

2 Answers

up vote 5 down vote accepted

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

share|improve this answer
    
You have forgotten the subsubsection: \renewcommand*{\l@subsubsection}[2]{% \stdl@subsection{\hspace{1cm}\textcolor{TheColorIWant}{#1}}{\textcolor{TheColorI‌​Want}{#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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.