我想在目录中添加指定长度的线段来分隔两个不同的部分。(我搜索过这个网站,但只能找到一个添加全长水平线的示例。)
我对完成此操作的难度感到惊讶,因为 \rule{1.25in}{.15mm} 在文档本身中运行良好。但是,当我尝试添加以下内容时,我收到一条错误消息:
\addcontentsline{toc}{\rule{1.25in}{.15mm}}
考虑这个工作示例:
\documentclass[12pt]{book}
\usepackage{tocloft}
\renewcommand\cftchapafterpnum{\vskip 12pt} %set space after each Chapter in the Table of Contents
\renewcommand\cftsecafterpnum{\vskip 8pt} %set space after each Section in the Table of Contents
\renewcommand\cftsubsecafterpnum{\vskip 8pt} %set space after each Subsection in the Table of Contents
% Centering Table of Contents
\renewcommand{\cfttoctitlefont}{\hfill\Huge\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\addtocontents{toc}{\protect\thispagestyle{empty}}
\tableofcontents
\addcontentsline{toc}{chapter}{Chapter 1}
\addcontentsline{toc}{chapter}{Chapter 2}
\addcontentsline{toc}{chapter}{Chapter 3}
\addtocontents{toc}{\vspace{\normalbaselineskip}}
%\addcontentsline{toc}{\rule{1.25in}{.15mm}}
\addtocontents{toc}{\vspace{\normalbaselineskip}}
\addcontentsline{toc}{chapter}{Next Part of the Table of Contents}
\end{document}
产生输出
但是,当我使用 \addcontentsline{toc}{\rule{1.25in}{.15mm}} 命令运行代码时,我收到错误消息“\reserved@a 的参数有一个额外的}。\addtocontents”。
输出结果如下:
再次,我想向目录中添加指定长度的水平线段---但我到目前为止尝试的所有方法都失败了。有人能告诉我我做错了什么以及如何纠正吗?谢谢。
答案1
您需要使用\addtocontents
而不是并在您的之后\addcontentsline
插入以便成功处理 ToC 内容。\par
\rule
\documentclass{book}
\usepackage{tocloft}
\setlength{\cftbeforechapskip}{12pt}%set space after each Chapter in the Table of Contents
\setlength{\cftbeforesecskip}{8pt} %set space after each Section in the Table of Contents
\setlength{\cftbeforesubsecskip}{8pt} %set space after each Subsection in the Table of Contents
% Centering Table of Contents
\renewcommand{\cfttoctitlefont}{\hfill\Huge\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\tocloftpagestyle{empty}
\tableofcontents
\addcontentsline{toc}{chapter}{Chapter 1}
\addcontentsline{toc}{chapter}{Chapter 2}
\addcontentsline{toc}{chapter}{Chapter 3}
\addtocontents{toc}{\vspace{\normalbaselineskip}}
\addtocontents{toc}{\rule[.1\normalbaselineskip]{1.25in}{.15mm}\par}
\addtocontents{toc}{\vspace{\dimexpr\normalbaselineskip-\cftbeforechapskip}}
\addcontentsline{toc}{chapter}{Chapter 4}
\end{document}
我将其\rule
向上移动了一点,以便在两个章节标题之间垂直居中显示得更好。我还使用了默认的tocloft
垂直间距/跳过和页面样式,而不是手动将内容添加到目录中。不过这其实没那么重要。
答案2
这会将线精确地放置在 36pt 间隙的中间 ( \baselineskip=12pt
)。
\documentclass[12pt]{book}
\usepackage{tocloft}
\renewcommand\cftchapafterpnum{\vskip 12pt} %set space after each Chapter in the Table of Contents
\renewcommand\cftsecafterpnum{\vskip 8pt} %set space after each Section in the Table of Contents
\renewcommand\cftsubsecafterpnum{\vskip 8pt} %set space after each Subsection in the Table of Contents
% Centering Table of Contents
\renewcommand{\cfttoctitlefont}{\hfill\Huge\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}
\begin{document}
\addtocontents{toc}{\protect\thispagestyle{empty}}
\tableofcontents
\addcontentsline{toc}{chapter}{Chapter 1}
\addcontentsline{toc}{chapter}{Chapter 2}
\addcontentsline{toc}{chapter}{Chapter 3}
\addtocontents{toc}{\rule[\dimexpr 0.5\ht\strutbox-0.5\dp\strutbox]{1.25in}{.15mm}\vskip 12pt}
\addtocontents{toc}{Next Part of the Table of Contents}
\end{document}