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.

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}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}%
    }%
    {\endMakeFramed}

    \makeatletter
        \def\@chapter[#1]#2{%
            \ifnum \c@secnumdepth >\m@ne
                \if@mainmatter
                    \refstepcounter{chapter}%
                    \typeout{\@chapapp\space\thechapter.}%
                    \addtocontents{toc}%
                    {%
                        {\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
                    \addcontentsline{toc}{chapter}{#1}%
                \fi
            \else
                \addcontentsline{toc}{chapter}{#1}%
            \fi
            \chaptermark{#1}%
            \addtocontents{lof}{\protect\addvspace{10\p@}}%
            \addtocontents{lot}{\protect\addvspace{10\p@}}%
            \if@twocolumn
                \@topnewpage[\@makechapterhead{#2}]%
            \else
                \@makechapterhead{#2}%
                \@afterheading
            \fi%
        }
    \makeatother

% << WARNING ! >>
%
% \renewcommand{\contentsname} does not work with babel.
% With babel we have to use 
%    \addto\captionsenglish{\def\contentsname{...}}
% or
%    \addto\extrasenglish{\def\contentsname{...}}.
    \renewcommand{\contentsname}{\sffamily \textcolor{\color{myred}}{Another title for the table of contents}}


\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}
share|improve this question
    
It seems that titlesec can't do that. –  brownian Nov 23 '11 at 14:53
add comment

3 Answers

up vote 14 down vote accepted

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}}%
        \MakeFramed {\advance\hsize-\width \FrameRestore}%
    }%
    {\endMakeFramed}

    \makeatletter
        \def\@chapter[#1]#2{%
            \ifnum \c@secnumdepth >\m@ne
                \if@mainmatter
                    \refstepcounter{chapter}%
                    \typeout{\@chapapp\space\thechapter.}%
                    \addtocontents{toc}%
                    {%
                        {\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
                    \addcontentsline{toc}{chapter}{#1}%
                \fi
            \else
                \addcontentsline{toc}{chapter}{#1}%
            \fi
            \chaptermark{#1}%
            \addtocontents{lof}{\protect\addvspace{10\p@}}%
            \addtocontents{lot}{\protect\addvspace{10\p@}}%
            \if@twocolumn
                \@topnewpage[\@makechapterhead{#2}]%
            \else
                \@makechapterhead{#2}%
                \@afterheading
            \fi%
        }
\AtBeginDocument{\renewcommand\contentsname{Another title for the Table of Contents}}
\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 
%    \addto\captionsenglish{\def\contentsname{...}}
% or
%    \addto\extrasenglish{\def\contentsname{...}}.


\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.

enter image description here

share|improve this answer
    
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
add comment

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}
\renewcommand{\contentsname}{\sffamily\textcolor{red}{Another title for the table of contents}}
\begin{document}
\tableofcontents
\section{Header of first and only section}
\end{document}

enter image description here

share|improve this answer
    
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
\addtokomafont{disposition}{\color{red}}

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

\AtBeginDocument{%
  \renewcaptionname{english}\contentsname{Another title for the table of contents}%
}

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
\addtokomafont{chapter}{\color{red}}
\addtokomafont{section}{\color{blue}}

And if only the table of contents should be red, you may use

\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
\BeforeTOCHead[toc]{%
  \addtokomafont{section}{\color{red}}% with scrartcl replace "chapter" by "section"
}
\AtBeginDocument{%
  \renewcaptionname{english}\contentsname{Another title for the table of contents}%
}

\usepackage{blindtext}% for demonstration only

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

top part of the result of the first page of the KOMA-Script example

share|improve this answer
    
Thanks for this proposition. –  projetmbc Nov 23 '11 at 17:04
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.