如何通过附加文档开头的自定义标签生成目录?

如何通过附加文档开头的自定义标签生成目录?

我正在附加多页文档,但希望在目录中有指向每个文档第一页开头的链接。制作自定义目录的伪代码

  1. 在 pdf 文档的第一页添加自定义标签
  2. 将自定义标签添加到目录

代码在页面上添加页码而不减小 pdf 页面的大小,但对 pdf 页面开头的引用应用没有任何作用

\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref} % for links in TOC
% https://tex.stackexchange.com/q/56316/13173
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt} % Werner, remove top margin border
% https://tex.stackexchange.com/a/338919/13173
\usepackage{letltxmacro}
\LetLtxMacro\oldincludepdf\includepdf
\renewcommand{\includepdf}[2][]{%
  \oldincludepdf[pagecommand={‌​\thispagestyle{fancy‌​}},#1]{#2}}

\begin{document}

% TODO add custom labels to TOC 
\tableofcontents
% Pseudocode
% \addcontentsline{toc}{someLabelForPdf}{\listPdfBeginningLabels}

% TODO add here a label/... to the beginning of pdf page such that included in TOC
\includepdf[pages=1,pagecommand=\thispagestyle{plain}]{7.pdf}
% No reference to the rest
\includepdf[pages=2-,pagecommand=\thispagestyle{plain}]{7.pdf}

% TODO reference here
\includepdf[pages=1,pagecommand=\thispagestyle{plain}]{8.pdf}
% No reference to the rest
\includepdf[pages=2-,pagecommand=\thispagestyle{plain}]{8.pdf}

\end{document}

也许伪代码\addcontentsline{toc}{someLabelForPdf}{\listPdfBeginningLabels}可以起作用。

目录示例

Table of Contents
7               1
8               150

尝试 Samcarter 的提议

手动的Samcarter 所指的是关于以下内容,addtotoc因此我尝试了以下内容,但得到了以下输出

\includepdf[pages=-,pagecommand=\thispagestyle{plain},addtotoc={1}]{7.pdf}

输出

<use  "7.pdf" > <use  "7.pdf" > <use  "7.pdf"  page1> <use  "7.pdf"  page1>
[1] <use  "7.pdf"  page1> <use  "7.pdf"  page1>
Runaway argument?
addtotoc={1}]{9.pdf} \includepdf [pages=-,pagecommand=\thispagestyle \ETC.
! Paragraph ended before \AM@parse@toclisti was complete.
<to be read again> 
                   \par 
l.33 

手动的

addtotoc Adds an entry to the table of contents. This option requires five
arguments, separated by commas:
addtotoc={hpage number i,hsection i,hlevel i,hheading i,hlabel i}

重申 Christian 的提议

无需用pages=1和分隔pages=2-。链接中有一些细微的修饰

\documentclass{article}
\usepackage{pdfpages}
\usepackage[hidelinks]{hyperref}
\usepackage{xcolor}
\hypersetup{
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}
% https://tex.stackexchange.com/q/56316/13173
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt} % Werner, remove top margin border
% https://tex.stackexchange.com/a/338919/13173
\usepackage{letltxmacro}
\LetLtxMacro\oldincludepdf\includepdf
\renewcommand{\includepdf}[4][]{%
  \oldincludepdf[pagecommand={‌​\thispagestyle{fancy‌​}},addtotoc={#4,section,1,#2,#3}, #1]{#2}}

\begin{document}
\tableofcontents
\includepdf[pages=-,pagecommand=\thispagestyle{plain}]{8.pdf}{p1d8}{1}
\end{document}

操作系统:Debian 8.7
硬件:Asus Zenbook UX303UB
测试文件7.pdfhttp://www.texdoc.net/texmf-dist/doc/latex/pdfpages/pdfpages.pdf
测试文件8.pdfhttp://ctan.sharelatex.com/tex-archive/macros/latex/required/graphics/grfguide.pdf

答案1

这个addtotoc选择有点棘手!

addtotoc={page-number,sectiontype,level,heading,label}的正确用法是addtotoc

页码必须与选项给出的页码相匹配pages=

  • sectiontype 可以是sectionsubsection等等。
  • level 是部分级别,即 1 表示section,2 代表subsection等等(照例)
  • 标题是目录中出现的名称

  • 标签是应用于此包含的 pdf 文件的标签(可以留空


7.pdf8.pdf生成以下文件(并重命名)

\documentclass{article}

\usepackage{blindtext}

\begin{document}
\section{Beginning \jobname}
\blindtext[50]
\end{document}

这是运行的代码。

\documentclass{article}
\usepackage{pdfpages}
\usepackage{hyperref} % for links in TOC
% https://tex.stackexchange.com/q/56316/13173
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt} % Werner, remove top margin border
% https://tex.stackexchange.com/a/338919/13173
\usepackage{letltxmacro}
\LetLtxMacro\oldincludepdf\includepdf
\renewcommand{\includepdf}[4][]{%
  \oldincludepdf[pagecommand={‌​\thispagestyle{fancy‌​}},addtotoc={#4,section,1,#2,#3}, #1]{#2}}

\begin{document}

% TODO add custom labels to TOC 
\tableofcontents
% Pseudocode
% \addcontentsline{toc}{someLabelForPdf}{\listPdfBeginningLabels}

% TODO add here a label/... to the beginning of pdf page such that included in TOC
\includepdf[pages=1,pagecommand=\thispagestyle{plain}]{7.pdf}{firstpage7pdf}{1}
% No reference to the rest
\includepdf[pages=2-,pagecommand=\thispagestyle{plain}]{7.pdf}{remainingpages7pdf}{2}

% TODO reference here
\includepdf[pages=1,pagecommand=\thispagestyle{plain}]{8.pdf}{firstpage8pdf}{1}
% No reference to the rest
\includepdf[pages=2-,pagecommand=\thispagestyle{plain}]{8.pdf}{remainginpages8pdf}{2}

\end{document}

使用键值接口进行更新

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

% Switch to expl3 - Syntax

% Define a storing place for the option values
\prop_new:N \l_leo_option_prop

% keys for the new functionality
\keys_define:nn {LEO} {%
   page .code:n = {\prop_put:Nnn \l_leo_option_prop {page} {#1}},
  section .code:n = {\prop_put:Nnn \l_leo_option_prop {section} {#1}},
  level .code:n = {\prop_put:Nnn \l_leo_option_prop {level} {#1}},
  heading .code:n = {\prop_put:Nnx \l_leo_option_prop {heading} {#1}},
  label .code:n = {\prop_put:Nnn \l_leo_option_prop {label} {#1}},
}

%  Keys setting 
\cs_new:Npn \SetupPdfInclude #1 {%
  \keys_set:nn {LEO} {#1}
}

% Get some key value 
\cs_new:Npn \retrieveoption #1 {%
  \prop_item:Nn \l_leo_option_prop {#1}
}

\ExplSyntaxOff


\usepackage{pdfpages}
\usepackage{hyperref} % for links in TOC
% https://tex.stackexchange.com/q/56316/13173
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt} % Werner, remove top margin border
% https://tex.stackexchange.com/a/338919/13173
\usepackage{letltxmacro}


\makeatletter
\LetLtxMacro\pdfpages@@includepdf\includepdf

\RenewDocumentCommand{\includepdf}{O{}mo}{%
  \begingroup
  \IfValueTF{#3}{% Is there a 3rd argument? Yes
    % Process the keys for the \includepdf first in order to get the value of 'pages=...' option
    % This is stored to \AM@pagestemp (see pdfpages.sty)
    \setkeys{pdfpages}{#1}%
    \SetupPdfInclude{label={label#2\AM@pagestemp},page={\AM@pagestemp}, heading={#2},#3}
    % Call the old command with the options for addtotoc
    \pdfpages@@includepdf[pagecommand={\thispagestyle{fancy}},addtotoc={\retrieveoption{page},\retrieveoption{section},\retrieveoption{level},\retrieveoption{heading},\retrieveoption{label}},#1]{#2}
  }{% No, no 3rd. argument}
    \pdfpages@@includepdf[pagecommand={\thispagestyle{fancy}},#1]{#2}
  }%
  \endgroup% Prevent leaking of key values to the next call of the command
}
\makeatother



% Set some default values
\SetupPdfInclude{page=1,section=section,level=1}

\begin{document}

% TODO add custom labels to TOC 
\tableofcontents
% Pseudocode
% \addcontentsline{toc}{someLabelForPdf}{\listPdfBeginningLabels}

\includepdf[pagecommand=\thispagestyle{plain}]{7.pdf}[label=firstpage7pdf]
\includepdf[pages=2-,pagecommand=\thispagestyle{plain}]{7.pdf}[page=2,heading={\retrieveoption{heading} continued}]

\includepdf[pages=1,pagecommand=\thispagestyle{plain}]{8.pdf}[label=firstpage8pdf]
\includepdf[pages=2-,pagecommand=\thispagestyle{plain}]{8.pdf}[heading={\retrieveoption{heading} continued}]

\end{document}

几个键值对应到addtotoc选项中的条目,这样做的好处是,精确的顺序是由代码完成的,而不是通过使用规范。

默认情况下heading将使用强制参数\includepdf,即#2

相关内容