[Tex/LaTex] TOC entries and labels for included PDF pages

hyperrefpdfpagessectioningtable of contents

I am embedding full pages from another PDF document (using the pdfpages package), but I would like to be able to declare my own section headings before each embedded document (for e.g. hyperlinking / TOC purposes). pdfpages has an experimental feature which lets me do this, but the problem is that I want to put all of these embedded documents inside a section, so I need to insert a section heading and a subsection heading on the first document I embed. Unfortunately pdfpages doesn't seem to be able to do this. Is there a way for me to do this?

Edit: Thank you for the suggestions. I am also in need of something similar for general labels — at the moment I am abusing the addtolist option of \includePDF but the objects I'm labelling aren't really figures or tables.

Best Answer

You can create entries in the table of contents (TOC) when importing a PDF. Here is an example:

\documentclass{article}
\usepackage{pdfpages}

\begin{document}
\tableofcontents    
\includepdf[pages=-,addtotoc={
     1,section,1,First Section Entry,p1,   
     1,subsection,1,Subsection Entry,p2,
     2,section,1,Second Section Entry,p3}]
     {publishing-logo+layout.pdf}         
\end{document}

Parameters for each TOC entry are:

  • Page number relative to the first page of the included document. Caveat: with pages={3-10}, the smallest possible number would be 3.
  • Level for the TOC entry
  • Depth of section (1 for section, 2 for subsection, etc.)
  • TOC entry
  • Label for the entry

You can create two entries for a single page on different levels (like section and subsection). In the example, the first two entries go to the same page. A label (like p2) can be used to refer to a page that is part of the included PDF.

Unfortunately error messages from pdfpages are not helpful when the above list contains errors. I encountered the following error, which was hard to spot:

  • With pages={3-10} the first entry in an addtotoc record must be 3 or larger. It was 1 in my case but the error message wasn't pointing to it.