精美的目录

精美的目录

我怎样才能做出ToC如下的样子?

在此处输入图片描述 梅威瑟:

‎\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}‎

答案1

第一个试用版,需要对章节标题行进行更多设置。(我使用了传统\addcontentsline方式,而不是titlesec

章节行不要太长!我只添加了一行来显示这样做的负面影响。

\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}

在此处输入图片描述

更新间距更好

\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}

vietnam软件包更新

\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}

在此处输入图片描述

相关内容