Table of Contents – Fancy Table of Contents

table of contents

How can I make a ToC like the following?

enter image description here
MWE:

‎\documentclass[10pt,a4paper,openany]{book}‎‎
‎\usepackage{titletoc,xcolor}‎
‎‎
\titlecontents{chapter}% <section-type>
[0pt]% <left>
{}% <above-code>
{\fcolorbox{blue}{blue}{\color{white}\bfseries\chaptername~ \thecontentslabel}}% <numbered-entry-format>
{}% <numberless-entry-format>
{\bfseries\hfill\contentspage}% <filler-page-format>

\begin{document}‎
‎\tableofcontents‎‎
‎
‎\chapter{First Chapter}‎
‎\section{First Section}‎‎‎
‎\section{Second Section}‎‎
‎
‎\chapter{Second Chapter}‎
‎\section{First Section}‎‎
‎\section{Second Section}‎‎
sample
\end{document}‎

Best Answer

A first trial version, requires some more setup for the chapter title line. (I used the traditional \addcontentsline way, not titlesec)

Don't use too long lines for the chapter lines! I've added just one to show the negative effect of this.

\documentclass[10pt,a4paper,openany]{book}
\usepackage{tocloft}

\usepackage[x11names,dvipsnames,svgnames]{xcolor}

\usepackage[most]{tcolorbox}


\newtcbox{\chapternumberbox}[1][]{%
  colback=blue!50!black,
  colupper={white},
  fontupper={\bfseries\large},
  valign=center,
  size=small,
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}




\newtcolorbox{chapternamebox}[1][]{%
  enhanced jigsaw,
  valign=center,
  interior style={left color={white!30!blue},right color={white!90!blue}},
  colframe={gray},
  boxrule={1pt},
  colupper={black},
  size=small,
  fontupper={\bfseries\large},
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

% A wrapper command
\newcommand{\mychapternamebox}[3][]{%
  \begin{chapternamebox}[#1]
    #2\hfill#3%
  \end{chapternamebox}%
}


\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
}{%
  \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
  \addcontentsline{toc}{chapter}{\protect\chapternumberbox{\protect\chaptername{} \thechapter}\protect\mychapternamebox[width={9.8cm}]{#1}{\thepage}}%%
  \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%
}{}{}
\makeatother

\begin{document}
\tableofcontents

\chapter{First Chapter}
\section{First Section}‎‎
\section{Second Section}
\chapter{Second Chapter}
\section{First Section}
\section{Second Section}
\chapter{Third Chapter with an excessively long title that should be avoided!}‎‎
\end{document}

enter image description here

Update with better spacing

\documentclass[10pt,a4paper,openany]{book}
\usepackage{tocloft}
\usepackage[x11names,dvipsnames,svgnames]{xcolor}

\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{array}

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\makeatletter


\tcbset{%
  chapternumberbox/.style={%
    colframe=black,
    colback={blue!60!white},
    colupper={white},
    fontupper={\bfseries\normalsize},
    halign=center,
    valign=center,
    size=small,
    nobeforeafter,
    equal height group=chaptertoc,
    left skip=0pt,
    right skip=0pt,
  },
  chapternamebox/.style={%
    left skip=0pt,
    right skip=0pt,
    enhanced jigsaw,
    valign=center,
    interior style={left color={blue!60!white},right color={blue!15!white}},
    colframe={black},
    boxrule={1pt},
    colupper={black},
    size=small,
    fontupper={\bfseries\normalsize},
    nobeforeafter,
    equal height group=chaptertoc,
  }
}

\newtcolorbox{chapternumberboxinternal}[1][]{%
  chapternumberbox,
  #1
}

\newtcolorbox{chapternamebox}[1][]{%
  chapternamebox,
  #1,
}

\NewDocumentCommand{\chapternumberbox}{O{}m}{%
  \begin{chapternumberboxinternal}[#1,width={\cftchapnumwidth}]%-\kvtcb@left@rule-\kvtcb@right@rule}]
    #2%
  \end{chapternumberboxinternal}%
}



\NewDocumentCommand{\mychapternamebox}{O{}+mm}{%
  \begin{chapternamebox}[#1,width={\dimexpr\tocwidth-\cftchapnumwidth+\marginparsep}]
    \begin{tabular}{@{}p{\kvtcb@leftupper}@{}@{}p{\dimexpr\tocwidth-\cftchapnumwidth-\@pnumwidth-\kvtcb@leftupper-\marginparsep}@{}@{}R{\dimexpr\@pnumwidth}@{}@{}p{\kvtcb@rightupper}@{}}
      &  #2&#3
    \end{tabular}
  \end{chapternamebox}%
}


\usepackage{xpatch}
\usepackage{xprintlen}

\newlength\tocwidth

\renewcommand{\cftchapnumwidth}{2.5cm}
\setlength{\cftchapindent}{0pt}



\AtBeginDocument{%
  \setlength{\tocwidth}{\linewidth}
}



\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
}{%
  \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
  \addcontentsline{toc}{chapter}{\chapternumberbox{\protect\chaptername\ \thechapter}\protect\mychapternamebox[]{#1}{\thepage}}%%
  \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%
}{}{}
\makeatother

\begin{document}

\tableofcontents

\chapter{First Chapter}
\section{First Section}‎‎
\section{Second Section}
\chapter{Second Chapter}
\section{First Section}
\section{Second Section}
\chapter[Theory on Brontosaurs]{Third Chapter with an excessively long title that should be avoided!}‎‎
\end{document}

Update for vietnam package

\documentclass[10pt,a4paper,openany]{book}

\usepackage{tocloft}
\usepackage[x11names,dvipsnames,svgnames]{xcolor}

\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{array}




\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\makeatletter


\tcbset{%
  chapternumberbox/.style={%
    colframe=black,
    colback={blue!60!white},
    colupper={white},
    fontupper={\bfseries\normalsize},
    halign=center,
    valign=center,
    size=small,
    nobeforeafter,
    equal height group=chaptertoc,
    left skip=0pt,
    right skip=0pt,
  },
  chapternamebox/.style={%
    left skip=0pt,
    right skip=0pt,
    enhanced jigsaw,
    valign=center,
    interior style={left color={blue!60!white},right color={blue!15!white}},
    colframe={black},
    boxrule={1pt},
    colupper={black},
    size=small,
    fontupper={\bfseries\normalsize},
    nobeforeafter,
    equal height group=chaptertoc,
  }
}

\newtcolorbox{chapternumberboxinternal}[1][]{%
  chapternumberbox,
  #1
}

\newtcolorbox{chapternamebox}[1][]{%
  chapternamebox,
  #1,
}

\NewDocumentCommand{\chapternumberbox}{O{}m}{%
  \begin{chapternumberboxinternal}[#1,width={\cftchapnumwidth}]%-\kvtcb@left@rule-\kvtcb@right@rule}]
    #2%
  \end{chapternumberboxinternal}%
}



\NewDocumentCommand{\mychapternamebox}{O{}+mm}{%
  \begin{chapternamebox}[#1,width={\dimexpr\tocwidth-\cftchapnumwidth+\marginparsep}]
    \begin{tabular}{@{}p{\kvtcb@leftupper}@{}@{}p{\dimexpr\tocwidth-\cftchapnumwidth-\@pnumwidth-\kvtcb@leftupper-\marginparsep}@{}@{}R{\dimexpr\@pnumwidth}@{}@{}p{\kvtcb@rightupper}@{}}
      &  #2&#3
    \end{tabular}
  \end{chapternamebox}%
}


\usepackage{xpatch}
\usepackage{xprintlen}

\newlength\tocwidth

\renewcommand{\cftchapnumwidth}{2.5cm}
\setlength{\cftchapindent}{0pt}



\AtBeginDocument{%
  \setlength{\tocwidth}{\linewidth}
}


\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
}{%
  \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
  \addcontentsline{toc}{chapter}{\protect\chapternumberbox{\protect\chaptername\ \thechapter}\protect\mychapternamebox[]{#1}{\thepage}}%%
  \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%
}{\typeout{Patch Success}}{\typeout{Patch Failure}}

\makeatother

\usepackage[utf8]{vietnam}
\usepackage[unicode]{hyperref}


\begin{document}

\tableofcontents

\chapter{First Chapter}
\section{First Section}
\section{Second Section}
\chapter{Second Chapter}
\typeout{Hello}
\section{First Section}
\section{Second Section}
\chapter[Theory on Brontosaurs]{Third Chapter with an excessively long title that should be avoided!}
\end{document}

enter image description here