# Unable to link to inserted pages with pdfpages

I would like to add labels to pages inserted with the pdfpages package. I sort of works right now, but it is not possible to link to the labels with \ref and \pageref commands. But 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 automatic.

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}.
\end{document}


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

-

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}.
\end{document}

-
Under certain conditions, using \includepdf[pages=-,link,linkname=testing,addtotoc={2,section,0,page,mypage}]{t‌​est.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 Aug 7 '11 at 17:32
Very nice answer, which solves my problem. Thx. – midtiby Aug 7 '11 at 17:41

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}
\refstepcounter{includepdfpage}
\stepcounter{currentpagecounter}
\label{#1.\thecurrentpagecounter}}
\newcommand{\modifiedincludepdf}[3]{
\setcounter{currentpagecounter}{0}

\begin{document}

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

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

\end{document}

-
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. – Martin Scharrer Aug 7 '11 at 20:33

I'm working with the code below that was posted to deal with multiple included pdfs. However my issue is that I'm dealing with some more complicated formatting for my document to: 1) move page numbers to the bottom right of the includedpdf documents, 2) include section headings at the top of the first page of some included pdfs (not all). So, this is a MWE of what I'm working with. I'm really not that good at coding and so I don't know how to manipulate the code below to account for the different arguments to \pagecommand (i.e. fancypage,section, subsection). Thanks, anybody!

\documentclass{article}
% !TEX encoding = UTF-8 Unicode
% !BIB TS-program = biber
\usepackage{polyglossia}
\usepackage{fontspec}
\usepackage{csquotes}
\usepackage{fancyhdr}
\setmainlanguage{english}
\usepackage{hyperref}
\usepackage{pdfpages}

\begin{document}

\section{Section 1}

This is my document text. I would like to have a page reference to the first page %
of the first document any_pdf about here \pageref{any_pdf_page1}
\includepdf[pages={1}, pagecommand=    {\thispagestyle{fancy} %
\subsection{Syllabi}\subsubsection{Subsection}}, scale=0.6]{any_pdf}
\includepdf[pages={2-}, pagecommand={\thispagestyle{fancy}}, scale=0.8]%
{any_pdf}{\thispagestyle{fancy}}, scale=0.8]{any_pdf}
\includepdf[pages=-, pagecommand={\thispagestyle{fancy}}, scale=0.8]%
{a_second_pdf}
\end{document}


Previous code from above

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

\newcounter{includepdfpage}
\newcounter{currentpagecounter}
\refstepcounter{includepdfpage}
\stepcounter{currentpagecounter}
\label{#1.\thecurrentpagecounter}}
\newcommand{\modifiedincludepdf}[3]{
\setcounter{currentpagecounter}{0}