我正在包含 PDF 文档,并用它tocloft \cftaddnumtitleline
来重新创建所包含文档中的目录元素。我期望目录如下所示:
我能够通过添加标有的行来创建预期的目录%% <--- HERE
,这表明cftaddnumtitleline
除非后面跟着普通的目录条目,否则不会向目录添加任何附加内容。
为什么会这样?有没有更好的方法可以让目录条目出现,而不使用我找到的解决方法(其副作用是添加空白页)?
最小示例:
\documentclass[twoside,12pt,openany]{book}
\usepackage{pdfpages}
\usepackage{fancyhdr}
\usepackage{tocloft}
\newcommand{\addsection}[3]{\addtocontents{toc}{\protect\contentsline{section}{\protect\numberline{#1}#2}{#3}}}
\newcommand{\addsubsection}[3]{\addtocontents{toc}{\protect\contentsline{subsection}{\protect\numberline{#1}#2}{#3}}}
%% Headings for chapter
\fancypagestyle{chapter}
{
\fancyhf{}
\renewcommand{\headrulewidth}{1pt}
\fancyhead[RO]{\bfseries{\MakeUppercase{Chapter title}}}
\fancyhead[LE]{\bfseries{Chapter Number}}
\fancyfoot[C]{\thepage}
}
%% Insert a blank page for oddside start
\newcommand*\cleartorightpage{%
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
\fancyfoot[C]{\thepage}
\clearpage
\hbox{}\newpage
}
\begin{document}
\tableofcontents
\cleartorightpage
\pagestyle{chapter}
\chapter{Chapter One}
\includepdf[pages={2-4}, pagecommand={}]{Include}
\cftaddnumtitleline{toc}{section}{1}{Example Section 1}{4}
\cftaddnumtitleline{toc}{section}{2}{Example Section 2}{5}
\cftaddnumtitleline{toc}{section}{3}{Example Section 3}{6}
\pagestyle{chapter}
\chapter{Chapter Two}
\includepdf[pages={2-4}, pagecommand={}]{Include}
\cftaddnumtitleline{toc}{section}{1}{Example Section 1}{8}
\cftaddnumtitleline{toc}{section}{2}{Example Section 2}{9}
\cftaddnumtitleline{toc}{section}{3}{Example Section 3}{10}
\chapter*{} %% <--- HERE: Why is this needed?
\end{document}
(这将生成文件 Include.pdf,用于最小示例):
\documentclass[twoside,12pt,openany]{article}
\usepackage{setspace}
\usepackage{lipsum}
\newcommand*\cleartoleftpage{%
\clearpage
\ifodd\value{page}\hbox{}\newpage\fi
}
\begin{document}
\pagestyle{empty}
\cleartoleftpage % Start evenside.
\doublespacing
\lipsum
\end{document}
答案1
这是我通常做的事情(我删除了很多似乎与这个问题无关的代码)。
\documentclass[twoside,12pt,openany]{book}
\usepackage{pdfpages}
\usepackage{tocloft}
\begin{document}
\tableofcontents
\chapter{Chapter One}
\includepdf[pages={2-4},
addtotoc={% first number is the page number in the included PDF
2,section,1,Example Section 1,sec11,%
3,section,1,Example Section 2,sec12,%
4,section,1,Example Section 3,sec13%
},
]{Include}
A reference: \ref{sec12} on page~\pageref{sec12}
\chapter{Chapter Two}
\includepdf[pages={2-4},%pagecommand={}
addtotoc={% first number is the page number in the included PDF
2,section,1,Example Section 1,sec21,%
3,section,1,Example Section 2,sec22,%
4,section,1,Example Section 3,sec23%
},
]{Include}
\end{document}
这还有一个好处,就是它独立于文档中的页码(您似乎在原始代码中对页码进行了硬编码)。