20

I would like to add labels to pages inserted with the pdfpages package. It sort of works right now, but it is not possible to link to the labels with \ref and \pageref commands. In spite of that not working, it is possible to link to the page with the hyperlink command from hyperref.

The goal is to compile a list of things to read in a course and then later say 'read from page xx to yy'. The pagenumbers should of course be determined automatically.

In the document below, the pageref expands to ?? while the text Hello links to the correct page.

\documentclass{article}
\usepackage{hyperref}
\usepackage{pdfpages}
\begin{document}
See page \pageref{testing.1}.
\hyperlink{testing.1}{Hello.}
\includepdf[pages=-,link,linkname=testing]{test.pdf}
\end{document}

For reference, the inserted document (test.pdf) can be any valid PDF document.

3 Answers 3

35

The inserted link is a hyper-link, not a LaTeX \label. Therefore the \hyperlink macro works but not the \pageref. I don't think you can get the page information from a hyper-link, so I think you need to add \labels manually. This can be done best using the pagecommand key and a custom counter:

\documentclass{article}
\usepackage{hyperref}
\usepackage{pdfpages}
\newcounter{includepdfpage}
\begin{document}
See page \pageref{testing.1} till \pageref{testing.10}.
\hyperlink{testing.1}{Hello.}
\includepdf[pages=-,link,linkname=testing,pagecommand={\refstepcounter{includepdfpage}\label{testing.\theincludepdfpage}}]{test.pdf}
\end{document}
1
  • 1
    Under certain conditions, using \includepdf[pages=-,link,linkname=testing,addtotoc={2,section,0,page,mypage}]{test.pdf} provides a \pageref{mypage} to page 2 - and only that particular page - in test.pdf. However, it also creates a table of contents (with bookmarks). @Martin's answer is the bomb!
    – Werner
    Commented Aug 7, 2011 at 17:32
14

Just for reference, here is the solution I ended up using. It is based on the answer of Martin Scharrer, but modified to work with several inserted documents.

\documentclass{article}
\usepackage{hyperref}
\usepackage{pdfpages}

\newcounter{includepdfpage}
\newcounter{currentpagecounter}
\newcommand{\addlabelstoallincludedpages}[1]{
   \refstepcounter{includepdfpage}
   \stepcounter{currentpagecounter}
   \ifnum\value{currentpagecounter}=1 %
     \label{#1}
   \fi
   \label{#1.\thecurrentpagecounter}}
\newcommand{\modifiedincludepdf}[3]{
    \setcounter{currentpagecounter}{0}
    \includepdf[pages=#1,pagecommand=\addlabelstoallincludedpages{#2}]{#3}}

\begin{document}

See page \pageref{one.1} till \pageref{two.3}.

See beginning of included PDF \pageref{one}.

\modifiedincludepdf{-}{one}{test.pdf}
\modifiedincludepdf{-}{two}{test.pdf}

\end{document}
3
  • 1
    You normally need to use % at the end of each line ending in { and } (or any other non-macro) to avoid spaces. Here they won't be seen anyway. Commented Aug 7, 2011 at 20:33
  • How can you use \hyperlink with this solution to have not only the single page number clickable bu also some custom text ? Commented Mar 27, 2018 at 14:24
  • 1
    You can use the hyperref command from the package with the same name. The command is used like \hyperref[label_name]{''link text''}. So try to do the following: \hyperref[two.2]{testing}.
    – midtiby
    Commented Apr 3, 2018 at 8:31
2

Maybe late to the party.

Iterating on @martin-scharrer's idea, we can use pdfpages' variable \AM@linkname, so the user may opt to not change the linkname.

\documentclass{article}
\usepackage{hyperref}
\usepackage{pdfpages}
\newcounter{includepdfpage}

\makeatletter
\newcommand{\stepincluded}{
  \refstepcounter{includepdfpage}%
  \label{\AM@linkname.\theincludepdfpage}%
}
\makeatother

\begin{document}
See page \pageref{test.pdf.1} till \pageref{test.pdf.10}.
\hyperlink{test.pdf.1}{Hello.}
\includepdf[pages=-,link,pagecommand={\stepincluded}]{test.pdf}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .