格式化 TOC 中未编号的章节

Formatting unnumbered chapters in TOC

我有一份包含编号和未编号章节的文档。为了在 TOC 中将它们彼此区分开来,我希望未编号的章节以斜体显示。我的 MWE 适用于章节标题 - 如何将相应的页码设置为斜体?

此外,是否可以将第 1 部分条目居中?

\documentclass[a4paper, 12pt]{report}

\usepackage[titles]{tocloft}

\begin{document}
\tableofcontents

\part{Part 1}

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addcontentsline{toc}{chapter}{\textit{Unnumbered chapter}}

\end{document}

您可以使用 \addtocontents{toc} 手动编写 \addcontentsline 自然完成的内容:

\documentclass{report}

\usepackage[titles]{tocloft}

\begin{document}

\tableofcontents

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addtocontents{toc}
  {\protect\contentsline{chapter}{\textit{Unnumbered chapter}}{\textit{\thepage}}}

\end{document}

以上应该适用于 \chapters,因为它们通常设置在新页面上,因此 \thepage 会产生正确的值。但是,它不适用于 hyperref.

或者,定义一种名为 chapterstar 的新型目录条目:

\documentclass{report}

\usepackage[titles]{tocloft}
\usepackage{etoolbox}

\makeatletter
\let\l@chapterstar\l@chapter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\l@chapterstar}{\cftchapfont}{\cftchapstarfont}{}{}% Insert starred chapter font
\patchcmd{\l@chapterstar}{#2}{\cftchapstarpagefont #2}{}{}% Insert starred chapter page number font
\makeatother

\newcommand{\cftchapstarfont}{\cftchapfont\itshape}
\newcommand{\cftchapstarpagefont}{\cftchappagefont\itshape}

\begin{document}

\tableofcontents

\chapter{Numbered chapter}

\chapter*{Unnumbered chapter}
\addcontentsline{toc}{chapterstar}{Unnumbered chapter}

\end{document}

以上解决方案适用于 hyperref 并且更通用。