use*_*648 3 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}
Run Code Online (Sandbox Code Playgroud)
您可以\addcontentsline使用手动编写自然完成的操作\addtocontents{toc}:
\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}
Run Code Online (Sandbox Code Playgroud)
以上应该适用于\chapters,因为它们通常设置在新页面上,因此\thepage会产生正确的值。但是,它不适用于hyperref.
或者,定义一种名为 的新型 ToC 条目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}
Run Code Online (Sandbox Code Playgroud)
上述解决方案适用于hyperref并且更通用。