4

I am appending documents of many pages, but would like to have links to the beginnings of each document first page in TOC. Pseudocode for making custom TOC

  1. Add custom label at the first page of pdf documents
  2. Add custom labels to TOC

Code which adds pagenumbering on pages without decreasing size of pdf-pages, but does nothing to the application of references at the beginning of pdf-pages

\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}

Maybe pseudocode \addcontentsline{toc}{someLabelForPdf}{\listPdfBeginningLabels} can work.

Example TOC

Table of Contents
7               1
8               150

Trying Samcarter's proposal

Manual where Samcarter is poiting to is about the following addtotoc so I try the following but I get the following output

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

Output

<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 

Manual

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}

Iterating Christian's proposal

No need to separate by pages=1 and pages=2-. Some minor cosmetic changes in links

\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}

OS: Debian 8.7
Hardware: Asus Zenbook UX303UB
Test file 7.pdf: http://www.texdoc.net/texmf-dist/doc/latex/pdfpages/pdfpages.pdf
Test file 8.pdf: http://ctan.sharelatex.com/tex-archive/macros/latex/required/graphics/grfguide.pdf

  • see pdfpages documentation, section "Hypertext options" and "Experimental options" – user36296 Apr 19 '17 at 14:04
  • 1
    @LéoLéopoldHertz준영: Can you provide us with some testing file 7.pdf or shall we invent some? ;-) – user31729 Apr 19 '17 at 17:42
  • 1
    Since addtotoc is tricky, one must provide the correct information, which is actually 5 parameters. Shall this be added as 5 additional arguments to \includepdf? – user31729 Apr 19 '17 at 18:05
4

The addtotoc option is a little bit tricky!

addtotoc={page-number,sectiontype,level,heading,label} is the correct usage of addtotoc,

where the page number must match the page number that is given with the pages= option.

  • sectiontype can be section, subsection etc.
  • level is the section level, i.e. 1 means section, 2 stands for subsection etc. (as usual)
  • heading is the name that appears in the ToC

  • label is the label that is applied to this included pdf file (can be left empty


The 7.pdf and 8.pdf have been generated with following file (and renamed)

\documentclass{article}

\usepackage{blindtext}

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

Here's the running code.

\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}

Update with key-value interface

\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}

The several key-values correspond to the entries in the addtotoc option, the advantage is, that the precise order is done by the code, not by usage specification.

By default heading will use the mandatory argument of \includepdf, i.e. #2.

  • @LéoLéopoldHertz준영: You can give an empty label, if remainingpages troubles you ;-) – user31729 Apr 19 '17 at 17:57
  • 2
    @LéoLéopoldHertz준영: Well, you have made the distinction between first page and pages=2 in your original code, for whatever reasons. – user31729 Apr 19 '17 at 18:46
  • 1
    @LéoLéopoldHertz준영: I would personally use a totally different style of the \includepdf macro. One user wants to have version A and scolds me of having provided version B or vice versa... – user31729 Apr 19 '17 at 19:05
  • 1
    @LéoLéopoldHertz준영: Updated – user31729 Apr 20 '17 at 5:59
  • 2
    @LéoLéopoldHertz준영: In fact, you do need the label for the addtotoc option since pdfpages adds an empty label (if this occurs more than once, LaTeX complains about multiple identical label (being empty then). In order to prevent this, I've added a standard label then. – user31729 Apr 20 '17 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.