我有一份文档,其中包含编号和未编号的章节。为了在 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}
以上应该适用于 \chapter
,因为它们通常设置在新页面上,因此 \thepage
会产生正确的值。但是,它不适用于 hyperref
.
或者,定义一种名为 chapterstar
的新型 ToC 条目:
\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
并且更通用。
关于latex - 格式化 TOC 中未编号的章节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41446607/