在工作中,我遇到了需要从 PDF 文档中插入页面(我使用了\usepackage{pdfpages}
和\includepdf[option]{doc.pdf}
),并且我想将这些页面添加到其中\tableofcontents
,并相应地增加部分编号。我用
\stepcounter{section}
\addcontentsline{toc}{section}{\thesection \hspace{7pt} <text>}
但是,当我尝试将这些功能停放到其中时,\newcommand
它返回一个错误。
定义命令:
\newcommand{\addtosection}
{
\stepcounter{#1}
\the\value{section}
\hspace{10pt}
}
命令执行:
\addcontentsline{toc}{section}{\addtosection <text>}
错误:
! Missing \endcsname inserted. <to be read again>
\csname\endcsname
l.24 ...pcounter{section} \addtosection Part Four}
The control sequence marked <to be read again> should not appear between \csname and \endcsname.
[1
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}
请帮忙~
我认为
我意识到当我输入时\stepcounter{section}
,\addcontentsline{toc}{section}{\stepcounter{section} <text>}
它会给出一个错误说
Missing \endcsname inserted. <to be read again> \csname\endcsname
我的应用程序的简化代码
\documentclass[11pt]{article}
\newcommand{\addtosection}
{%\stepcounter{#1}
\the\value{section}%
\hspace{10pt}%
}
\begin{document}
\tableofcontents
{---------------------------}
\section{Part One} % 1 Part One
\section*{Part Two}
\stepcounter{section}
\addcontentsline{toc}{section}{\thesection \hspace{7pt} Part Two} % 2 Part Two
\addcontentsline{toc}{section}{\addtosection Part Three} % 3 Part Three
\section{Part Four}
\end{document}
Error: % (on line 3 Part Three)
! Missing \endcsname inserted. <to be read again>
\csname\endcsname
l.24 ...pcounter{section} \addtosection Part Four}
The control sequence marked <to be read again> should not appear between \csname and \endcsname.
[1
{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}
答案1
我会使用(事实上确实会使用)自定义命令。例如,
\includepdftoc[<options>]{<title>}{<filename>}
将增加section
计数器,将<title>
其作为一个部分添加到目录中并包含<filename.pdf>
。目录中使用的编号将是 PDF 的第一页。如果使用可选参数,它将被传递给\includepdf
。
\documentclass[11pt]{article}
\usepackage{xparse,pdfpages}
\makeatletter
\NewDocumentCommand\includepdftoc { O {} m m }{%
\stepcounter{section}%
\def\addfirsttotoc{% from latex.ltx (modified)
\addcontentsline{toc}{section}{%
\protect\numberline{\csname thesection\endcsname}%
#2}}%
\def\makedynstyle{\addfirsttotoc\global\let\addfirsttotoc\relax}%
\includepdf[pagecommand={\makedynstyle},#1]{#3}%
}
\makeatother
\begin{document}
\tableofcontents
\bigskip
\hrule\bigskip
\section{Part One} % 1 Part One
\section*{Part Two}
\includepdftoc{Part Two}{example-image-a} % 2 Part Two
\includepdftoc[pages={-}]{Part Three}{forest-prooftrees-egs} % 3 Part Three
\section{Part Four}
\end{document}