# Formatting the title of the TOC

I would like for example to have a bold Arial like font and a red color for the title of the table of contents, and also I would like to change this title to for example "Another title for the table of contents".

The solution given below by Mico doesn't work in the following code. What are the changes to do ?

% Sources :
%   1) http://tex.stackexchange.com/questions/35825/pretty-table-of-contents
%   2) http://tex.stackexchange.com/questions/35903/formatting-the-title-of-the-toc

\documentclass{book}
\usepackage{xcolor}
\usepackage{framed}
%   \usepackage{helvet}

\definecolor{myred}{RGB}{160,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

\renewenvironment{leftbar}{%
\def\FrameCommand{{\color{myyellow}\vrule width 2pt depth 6pt} \hspace{10pt}}%
}%
{\endMakeFramed}

\makeatletter
\def\@chapter[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{%
{\protect\parbox{4.5em}{\hfill\Huge\color{myred}\bfseries\thepage}}%
\protect\hspace*{.5em}
\protect\parbox{\dimexpr\linewidth-4.3em\relax}{%
\protect\begin{leftbar}
{\scshape\small\chaptername~\thechapter}\\\sffamily#1%
\protect\end{leftbar}%
}
}%
\else
\fi
\else
\fi
\chaptermark{#1}%
\if@twocolumn
\else
\fi%
}
\makeatother

% << WARNING ! >>
%
% \renewcommand{\contentsname} does not work with babel.
% With babel we have to use
% or

\begin{document}

\tableofcontents

\chapter{Beginning to learn design with \LaTeX}

\chapter{Beginning to learn design with HTML}
\setcounter{page}{13}% just for the example

\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}

\chapter{Another chapter....}
\setcounter{page}{244}% just for the example

\end{document}

-
It seems that titlesec can't do that. –  brownian Nov 23 '11 at 14:53

You can achieve what you want without any spacial package (except maybe for the font change). You can simply redefine \tableofcontents as defined in book.cls. In the following example code I show how this redefinition would look like; feel free to add other features such as the font in the line marked with "% change here the format":

\documentclass{book}
\usepackage{xcolor}
\usepackage{framed}

\definecolor{myred}{RGB}{160,0,0}
\definecolor{myyellow}{RGB}{169,121,69}

\renewenvironment{leftbar}{%
\def\FrameCommand{{\color{myyellow}\vrule width 2pt depth 6pt} \hspace{10pt}}%
}%
{\endMakeFramed}

\makeatletter
\def\@chapter[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\if@mainmatter
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{%
{\protect\parbox{4.5em}{\hfill\Huge\color{myred}\bfseries\thepage}}%
\protect\hspace*{.5em}%
\protect\parbox{\dimexpr\linewidth-5em\relax}{%
\protect\begin{leftbar}
{\scshape\small\chaptername~\thechapter}\\\sffamily#1%
\protect\end{leftbar}%
}\par\noindent%
}%
\else
\fi
\else
\fi
\chaptermark{#1}%
\if@twocolumn
\else
\fi%
}
\renewcommand\tableofcontents{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{{\color{myred}\contentsname}% change here the formatting
\@mkboth{%
\MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
\@starttoc{toc}%
\if@restonecol\twocolumn\fi
}
\makeatother

% << WARNING ! >>
%
% \renewcommand{\contentsname} does not work with babel.
% With babel we have to use
% or

\begin{document}

\tableofcontents

\chapter{Beginning to learn design with \LaTeX}

\chapter{Beginning to learn design with HTML}
\setcounter{page}{13}% just for the example

\chapter{Beginning to learn design with HTML and some other text to span more than one line in the ToC}

\chapter{Another chapter....}
\setcounter{page}{244}% just for the example

\end{document}


I also corrected some spurious blank spaces in your code and some length in a \parbox.

-
Thanks for your help. :-) I'll try to do alone the design for the titles of section and co. I think that I will use \titlesec. –  projetmbc Nov 23 '11 at 18:09
This doest not work for starred version of \chapter such as \addcontentsline{toc}{chapter}{Preface} \chapter*{Preface} Could you aptch your code to allow for unnumbered chapters? –  Igor Kotelnikov Apr 6 '12 at 17:15
@Igor: The problem in this question has nothing to do with your comment. The question here was to change the formatting of the ToC title and that problem was solved. You already mentioned that \chapter* won't produce the desired result in another comment. No need to duplicate your remark. –  Gonzalo Medina Apr 6 '12 at 17:25

Remark: I wrote the answer below before the OP provided much more information as to what he/she needed. In the meantime, Gonzalo Medina has provided a much more extensive (and better!) answer which addresses the long list of objectives that need to be taken into account. :-)

I don't think it's necessary to use the titlesec package; however, given your description, you'll need the (x)color and helvet packages (assuming you're willing to use Helvetica instead of Arial...) -- don't forget to run the code (at least) twice:

\documentclass{article}
\usepackage{xcolor}
\usepackage{helvet}
\begin{document}
\tableofcontents
\section{Header of first and only section}
\end{document}


-
Note that there's also a package uarial (the font may be installed for TeX Live using getnonfreefonts). Note also, that \renewcommand{\contentsname} will not work with babel. With babel you have to use e.g. \addto\captionsenglish{\def\contentsname{…}} or \addto\extrasenglish{\def\contentsname{…}}. –  Schweinebacke Nov 23 '11 at 14:19
@Mico : your solution doesn't work with the special formatting of the TOC that I use. I don't understand why. I've updated my question. –  projetmbc Nov 23 '11 at 17:05
Changing the color in \renewcommand\contentsname is not a good idea since \contentsname is also used in some other places (the headers, for example) which will also inherit the new color, and this might not be desirable. –  Gonzalo Medina Nov 23 '11 at 17:43
@Schweinebacke -- thanks for providing information on how to obtain and install the uarial package. (I didn't mention it in my answer because I knew it's not available, by default, in quite a few TeX distributions.) Plus, I personally much prefer Helvetica to Arial. :-) –  Mico Nov 23 '11 at 18:12
@GonzaloMedina: I wrote my answer before the OP revised the question and provided a lot more information as to his/her objectives. E.g., I didn't know that he/she needed to use the book document class (which has header lines) rather than the article document class. –  Mico Nov 23 '11 at 18:49
show 1 more comment

If you are using a KOMA-Script class and want not only the the title of the table of contents but all titles in red arial you may use:

\usepackage[T1]{fontenc}% uarial is not available in OT1
\usepackage{uarial}% see http://ctan.org/pkg/urw-arial
\usepackage{xcolor}% see http://ctan.org/pkg/xcolor


The text may be changed using \renewcaptionname in this case:

\AtBeginDocument{%
}


If only the chapter headings should be red and the section headings should be blue and all the others should still be black you may use

\usepackage[T1]{fontenc}% uarial is not available in OT1
\usepackage{uarial}% see http://ctan.org/pkg/urw-arial
\usepackage{xcolor}% see http://ctan.org/pkg/xcolor


\BeforeTOCHead[toc]{%
\addtokomafont{chapter}{\color{red}}% with scrartcl replace "chapter" by "section"
}


Here is a MWE using scrartcl:

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{uarial}% see http://ctan.org/pkg/urw-arial
\usepackage{xcolor}% see http://ctan.org/pkg/xcolor
\addtokomafont{section}{\color{red}}% with scrartcl replace "chapter" by "section"
}
\AtBeginDocument{%
}

\usepackage{blindtext}% for demonstration only

\begin{document}
\tableofcontents
\blinddocument
\end{document}


-
Thanks for this proposition. –  projetmbc Nov 23 '11 at 17:04