[Tex/LaTex] Formatting the title of the TOC

formattingtable of contents

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}

Best Answer

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