Como gerar TOC por rótulos personalizados no início dos documentos anexados?

Como gerar TOC por rótulos personalizados no início dos documentos anexados?

Estou anexando documentos de muitas páginas, mas gostaria de ter links para o início da primeira página de cada documento no sumário. Pseudocódigo para criar um sumário personalizado

  1. Adicione um rótulo personalizado na primeira página dos documentos PDF
  2. Adicione rótulos personalizados ao sumário

Código que adiciona numeração de páginas sem diminuir o tamanho das páginas pdf, mas não faz nada na aplicação de referências no início das páginas 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}

Talvez o pseudocódigo \addcontentsline{toc}{someLabelForPdf}{\listPdfBeginningLabels}possa funcionar.

Exemplo de sumário

Table of Contents
7               1
8               150

Experimentando a proposta de Samcarter

Manualonde Samcarter está apontando é sobre o seguinte, addtotocentão tento o seguinte, mas obtenho a seguinte saída

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

Saída

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

Iterando a proposta de Christian

Não há necessidade de separar por pages=1e pages=2-. Algumas pequenas alterações cosméticas nos 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}

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

Responder1

A addtotocopção é um pouco complicada!

addtotoc={page-number,sectiontype,level,heading,label}é o uso correto de addtotoc,

onde o número da páginadevecorresponda ao número da página fornecido com a pages=opção.

  • sectiontype pode ser section, subsectionetc.
  • nível é o nível da seção, ou seja, 1 significa section, 2 significa subsectionetc. (como sempre)
  • cabeçalho é o nome que aparece no ToC

  • rótuloéo rótulo que é aplicado a este arquivo pdf incluído (pode ser deixado em branco


Os 7.pdfe 8.pdfforam gerados com o seguinte arquivo (e renomeados)

\documentclass{article}

\usepackage{blindtext}

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

Aqui está o código em execução.

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

Atualização com interface de valor-chave

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

Os diversos valores-chave correspondem às entradas da addtotocopção, a vantagem é que a ordem precisa é feita pelo código e não pela especificação de uso.

Por padrão headingusaremos o argumento obrigatório de \includepdf, ou seja #2, .

informação relacionada