latex - 格式化 TOC 中未编号的章节

标签 latex tableofcontents

我有一份文档,其中包含编号和未编号的章节。为了在 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 自然完成的操作:

enter image description here

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

相关文章:

r - 如何在 R Markdown 中添加目录?

python-3.x - 如何在 jupyter notebook markdown 中编写分段函数?

latex - 让 LaTeX 中的代码看起来*漂亮*

python - Sphinx 不会为标题生成侧边栏目录

python - 使用 ReportLab 向 PDF 正确添加页码和总页数

latex - 包括\lSTListoflistings 并在目录中引用它

html - 将无序列表转换为目录

javascript - 如何将 Latex 转换为 mathml

r - 让 Stargazer Column 标签打印在两行或三行上?

github - 潘多克 : set document title to first title