如何在自定义目录中包含未编号的章节?

如何在自定义目录中包含未编号的章节?

我正在尝试在我的 Latex 文件中实现自定义目录,并且我想包含未编号的章节Third Chapter

顺便说一句,在未编号的章节中,我希望具有相同的结构,但我希望只有一个具有相同颜色的空矩形,而不是第 1 章或第 2 章。

在此处输入图片描述

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

\usepackage[most]{tcolorbox}

\usepackage{tocloft}

\definecolor{darkmidnightblue}{rgb}{0.0, 0.2, 0.4}

\newtcbox{\chapternumberbox}[1][]{%
  colback=darkmidnightblue!50!darkmidnightblue,
  colupper={white},
  colframe={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={darkmidnightblue!30!darkmidnightblue},right color={darkmidnightblue!90!darkmidnightblue}},
  colframe={white},
  boxrule={1pt},
  colupper={white},
  size=small,
  fontupper={\bfseries\large},
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

\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}
%\cleardoublepage
\addcontentsline{toc}{chapter}{\protect\numberline{Second Chapter}}

\end{document}

答案1

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

\usepackage[most]{tcolorbox}

\usepackage{tocloft}

\definecolor{darkmidnightblue}{rgb}{0.0, 0.2, 0.4}

\newtcbox{\chapternumberbox}[1][]{%
  colback=darkmidnightblue!50!darkmidnightblue,
  colupper={white},
  colframe={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={darkmidnightblue!30!darkmidnightblue},right color={darkmidnightblue!90!darkmidnightblue}},
  colframe={white},
  boxrule={1pt},
  colupper={white},
  size=small,
  fontupper={\bfseries\large},
  nobeforeafter,
  equal height group=chaptertoc,
  #1,
}

\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}
%\cleardoublepage
  \addtocontents{toc}{\protect\cftpagenumbersoff{chapter}}
  \addcontentsline{toc}{chapter}{\protect\chapternumberbox{\hphantom{Chapter 2}}\protect\mychapternamebox[width={9.8cm}]{Third Chapter}{\thepage}}%%
  \addtocontents{toc}{\protect\cftpagenumberson{chapter}}%

\end{document}

在此处输入图片描述

相关内容