8

I want to customize my TOC starting with this example : How to customize the table of contents using TikZ?

I have done some changes, but I want more, here is my Code :

\documentclass[12pt,a4paper]{report}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[francais]{babel}
\usepackage[tmargin=2cm,rmargin=4cm,lmargin=4cm,bmargin=2cm]{geometry}
\usepackage{kpfonts}
\usepackage{tikz}
\definecolor{doc}{RGB}{0,60,110}
\usepackage{titletoc}
\contentsmargin{0cm}
\titlecontents{chapter}[0pc]
{\addvspace{30pt}%
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=doc!30,draw=doc!30] (-4,-.2) rectangle (-0.5,.5);%
\pgftext[left,x=-3.7cm,y=0.2cm]{\color{white}\Large\sc\bfseries chapitre\                                             
\thecontentslabel};%
\end{tikzpicture}\color{doc!40}\large\sc\bfseries}%
{}
{}
{\;\titlerule\;\large\sc\bfseries Page \thecontentspage
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=doc!25,draw=doc!20] (2pt,0) rectangle (6,0.1pt);
\end{tikzpicture}}%
\titlecontents{section}[2.4pc]
{\addvspace{1pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\hfill\small \thecontentspage}
[]
\titlecontents{subsection}[5pc]
{\addvspace{1pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\hfill\small \thecontentspage}
[]
\makeatletter
\renewcommand{\tableofcontents}{%
\chapter*{%
\vspace*{-20\p@}%
\begin{tikzpicture}[remember picture, overlay]%
\pgftext[right,x=15cm,y=0.2cm]{\color{doc!30}\Huge\sc\bfseries    
\contentsname};%
\draw[fill=doc!30,draw=doc!30] (13,-.75) rectangle (20,1);%
\clip (13,-.75) rectangle (20,1);
\pgftext[right,x=15cm,y=0.2cm]{\color{white}\Huge\sc\bfseries 
\contentsname};%
\end{tikzpicture}}%
\@starttoc{toc}}
\makeatother
\begin{document}
\tableofcontents
\chapter{Premier Chapitre}
\section{Titre Section 1}
\subsection{Titre sous Section 1}
\subsection{Titre sous Section 1}
\chapter{Deuxième Chapitre}
\section{Titre Section 1}
\subsection{Titre sous Section 1}
\section{Titre Section 1}
\subsection{Titre sous Section 2}
\chapter*{ Bibliographie}
\addcontentsline{toc}{chapter}{Bibliographie}
\end{document}  

Here is what I get :

enter image description here

And this is the changes I wanna make : enter image description here

1 Answer 1

5
  1. for the dots replace \hfill by \dotfill
  2. to remove the grey box in front of the bibliography, you could make use of the syntax of \titlecontents and define a separate format for unnumbered chapters:

    \titlecontents{⟨section⟩}[⟨left⟩]{⟨above-code⟩} {⟨numbered-entry-format⟩}{⟨numberless-entry-format⟩}{⟨filler-page-format⟩}[⟨below-code⟩] 
    
  3. The babel option francais is deprecated, use french instead.

\documentclass[12pt,a4paper]{report}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french]{babel}
\usepackage[tmargin=2cm,rmargin=4cm,lmargin=4cm,bmargin=2cm]{geometry}
\usepackage{kpfonts}
\usepackage{tikz}
\definecolor{doc}{RGB}{0,60,110}
\usepackage{titletoc}
\contentsmargin{0cm}


\titlecontents{chapter}[0pc]
{\addvspace{30pt}}%
{\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=doc!30,draw=doc!30] (-4,-.2) rectangle (-0.5,.5);%
\pgftext[left,x=-3.7cm,y=0.2cm]{\color{white}\Large\sc\bfseries chapitre\                                             
\thecontentslabel};%
\end{tikzpicture}\color{doc!40}\large\sc\bfseries}
{\color{doc!40}\large\sc\bfseries}
{\color{doc!40}\;\titlerule\;\large\sc\bfseries Page \thecontentspage
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=doc!25,draw=doc!20] (2pt,0) rectangle (6,0.1pt);
\end{tikzpicture}}%

\titlecontents{section}[2.4pc]
{\addvspace{1pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\dotfill\small \thecontentspage}
[]
\titlecontents{subsection}[5pc]
{\addvspace{1pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\dotfill\small \thecontentspage}
[]
\makeatletter

\renewcommand{\tableofcontents}{%
\chapter*{%
\vspace*{-20\p@}%
\begin{tikzpicture}[remember picture, overlay]%
\pgftext[right,x=15cm,y=0.2cm]{\color{doc!30}\Huge\sc\bfseries    
\contentsname};%
\draw[fill=doc!30,draw=doc!30] (13,-.75) rectangle (20,1);%
\clip (13,-.75) rectangle (20,1);
\pgftext[right,x=15cm,y=0.2cm]{\color{white}\Huge\sc\bfseries 
\contentsname};%
\end{tikzpicture}}%
\@starttoc{toc}}
\makeatother
\begin{document}
\tableofcontents
\chapter{Premier Chapitre}
\section{Titre Section 1}
\subsection{Titre sous Section 1}
\subsection{Titre sous Section 1}
\chapter{Deuxième Chapitre}
\section{Titre Section 1}
\subsection{Titre sous Section 1}
\section{Titre Section 1}
\subsection{Titre sous Section 2}
\chapter*{ Bibliographie}
\addcontentsline{toc}{chapter}{Bibliographie}
\end{document}  

enter image description here

1
  • Notice that when the chapter titles are longer, the page number will be in a second line. Apr 8, 2019 at 18:41

You must log in to answer this question.

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