如何在附加文件的開頭透過自訂標籤產生目錄?

如何在附加文件的開頭透過自訂標籤產生目錄?

我正在附加許多頁的文檔,但希望在目錄中提供指向每個文檔第一頁開頭的連結。用於製作自訂目錄的偽代碼

  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}

重複克里斯蒂安的提議

無需用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 可以是section等等subsection
  • level 是節級,即 1 表示section,2 表示subsection等(照常)
  • header 是出現在目錄中的名稱

  • label 是套用於此包含的 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

相關內容