2

I would like to create a table of contents where the chapters appear with a colorful background just like attached Image, and i hope to do this without tocloft ( without packages or with titletoc )

enter image description here

an attempt by Beginner

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{titletoc}
\usepackage[svgnames]{xcolor}

\renewcommand\thesection{\arabic{section}.}
\renewcommand\thesubsection{\arabic{subsection}.\arabic{section}}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[LE]{}
\fancyhead[RO]{}
\fancyhead[RE]{}
\fancyhead[LO]{}
\cfoot{\thepage}

\titlecontents*{chapter}
  [0pt]
  {\addvspace{1mm}}
  {\colorbox{MidnightBlue}{\textcolor{white}%       
  {\chaptername\ \thecontentslabel}}}
  {}
  {\colorbox{MidnightBlue}{\textcolor{white}%       
  {\hspace{7.5cm}\contentspage}}}

\makeatletter
\let\stdl@chapter\l@chapter
\renewcommand*{\l@chapter}[2]{%
  \stdl@chapter{\colorbox{MidnightBlue}{\textcolor{white}{#1}}}{\textcolor{MidnightBlue}{#2}}}
\makeatother

\begin{document}

\tableofcontents

\chapter{Chapter One}
\section{Section Two}
\subsection{Subsection Two}

\end{document}
2
  • Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Aug 15, 2014 at 10:33
  • Please explain why you wish to accomplish your objective without using a specialized LaTeX package.
    – Mico
    Aug 15, 2014 at 11:32

1 Answer 1

3

The easiest way apart from deep hacks, is to put a tighttcolorbox around the \l@chapter command, which is redefined and called internally with the standard values. However, this introduces some vertical spacing above and below the chapter toc entries.

The colours are hardcoded at the moment.

\documentclass{book}%
\usepackage{blindtext}%
\usepackage[svgnames]{xcolor}%

\usepackage{fancyhdr}%
\usepackage{tcolorbox}

\makeatletter
\let\standardlistchapter\l@chapter%

\renewcommand*{\l@chapter}[2]{%
\begin{tcolorbox}[colback={MidnightBlue},
  coltext={white},
  boxrule=0pt,
  boxsep=0pt,%
  left=0pt,top=-10pt,
  right=0pt,
  bottom=0pt,
  arc=0pt,
  auto outer arc]
\standardlistchapter{\textcolor{white}{#1}}{\textcolor{white}{#2}}%
\end{tcolorbox}%
}%




\renewcommand\thesection{\arabic{section}.}
\renewcommand\thesubsection{\arabic{subsection}.\arabic{section}}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\fancyhead[LE]{}
\fancyhead[RO]{}
\fancyhead[RE]{}
\fancyhead[LO]{}
\cfoot{\thepage}


\makeatother


\begin{document}
\tableofcontents%

\chapter{Chapter one}

\section{First}

\blindtext[5]

\chapter{Chapter two}

\section{First}

\blindtext[5]

\end{document}

enter image description here

You must log in to answer this question.

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