5

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}‎
  • 8
    -1: This really doesn't show any research effort. – Werner Sep 6 '14 at 7:56
  • 2
    The visual appearance of the toc is not really appealing -- Are you asking about the technique itself? – user31729 Sep 6 '14 at 7:59
  • 2
    I find the downvotes too harsh at the moment... – user31729 Sep 6 '14 at 8:24
  • 5
    (Our general policy has been to not down-vote beyond -1. Once it's at -1 then it's clear that there's something wrong with the question and it needs improving. If it doesn't get improved, it will be closed. Beyond -1 is therefore unnecessary. So @ChristianHupfer is right to find that. There's a meta question on this which I can't find right now.) – Loop Space Sep 6 '14 at 8:37
  • 2
    To the OP: You have piled up some questions with good answers, but you did not accept them. This is unsatisfactory... (No, I am not one of the users who gave an answer ;-)) – user31729 Sep 6 '14 at 8:41
12

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

  • I'll try to update this with \l@chapter instead of modifing \@chapter – user31729 Mar 27 '16 at 9:35
  • @touhami: Thanks. I'll try to update it continously (or more or less continuously ;-)) – user31729 Mar 27 '16 at 14:37
  • @ChristianHupfer I add \usepackage[utf8]{vietnam}, It doesn't work. – minhthien_2016 Dec 28 '17 at 23:53
  • @toandhsp: I don't speak or can't read Vietnamese – user31729 Dec 29 '17 at 0:04
  • @toandhsp: See the update at the end please. vietnam package must be loaded after the \xpatchcmd{\@chapter} statements. Compile three times to get the correct heights – user31729 Dec 29 '17 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.