Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using pdfpages to combine multiple pdf documents; I would also like a front 'contents' page for the new file, using hyperref to allow me to jump directly to the correct page. The 'addtotoc' option in pdfpages almost works... the link produced by that goes to the correct page, but a few lines down from the start. Any suggestions? The sample code below also shows a workaround using \hyperlink - but this file will potentially have to be maintained/updated by other people who may not be so familiar with \LaTeX, so the more that can be automated (ie the less changes needed for each additional file), the better.

\documentclass{article}
\usepackage{pdfpages}
\usepackage[pdfstartview=FitH,bookmarks=true,colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{bookmark}
\bookmarksetup{color=blue}
\setlength\parindent{0pt}

\begin{document}
\tableofcontents
\LARGE\hyperlink{Core1.pdf.1}{{Core 1}}\\
\LARGE\hyperlink{Core2.pdf.1}{{Core 2}}\\
\LARGE\hyperlink{Core3.pdf.1}{{Core 3}}\\
\LARGE\hyperlink{Core4.pdf.1}{{Core 4}}\\

\includepdf[pages=-,link,linkfit=FitH,addtotoc={1, section, 1, Core 1, Core1}]{Core1.pdf}
\bookmark[dest=Core1.pdf.1]{Core 1}
\includepdf[pages=-,link,linkfit=FitH]{Core2.pdf}
\bookmark[dest=Core2.pdf.1]{Core 2}
\includepdf[pages=-,link,linkfit=FitH]{Core3.pdf}
\bookmark[dest=Core3.pdf.1]{Core 3}
\includepdf[pages=-,link,linkfit=FitH]{Core4.pdf}
\bookmark[dest=Core4.pdf.1]{Core 4}
\end{document}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The default use of addtotoc in \includepdf creates a section and uses that as the anchor. The section is defined to be anonymous within the document but to have a name in the table of contents and bookmarks.

One idea I had would be to use the addtotoc mechanism to just add the entries to the table of contents (and thus the bookmarks), but with the hyperlinks pointing to the already defined anchors instead of the section anchors, like so:

\documentclass{article}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage[pdfstartview=FitH,bookmarks=true,colorlinks=true,linkcolor=blue]{hyperref}
\usepackage{bookmark}
\bookmarksetup{color=blue}
\setlength\parindent{0pt}

\setcounter{errorcontextlines}{\maxdimen}

\makeatletter
\newcommand*{\@pdfpagephantomsection}[2][]{\edef\@currentHref{\AM@linkname.\AM@page}\addcontentsline{toc}{section}{\protect\numberline{}#1}}
\makeatother

\begin{document}
\tableofcontents

\includepdf[pages=-,link,linkfit=FitH,addtotoc={1, pdfpagephantomsection, 1, Core 1, example-image-a}]{example-image-a.pdf}
\includepdf[pages=-,link,linkfit=FitH,addtotoc={1, pdfpagephantomsection, 1, Core 2, example-image-b}]{example-image-b.pdf}

\end{document}

This works because pdfpages only creates a section for TOC purposes if the named sectioning command (when prefixed with @) does not exist, otherwise it uses that command instead. Here, I called it pdfpagesphantomsection.

Note that I did not have the PDF files you named, so I used those provided in the mwe package instead.

share|improve this answer
    
That works perfectly! Many thanks for a speedy response. –  JustinT Dec 11 '12 at 8:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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