# How to correctly label an included PDF?

I have this:

\label{appendix:b}
\includepdf[scale=0.8,pages=1,pagecommand=\chapter{interview}]{interview/interview}
\includepdf[scale=0.8,pages=2-,pagecommand={}]{interview/interview}
\label{appendix:bslut}


And I'm calling it with cleveref:

\cpagerefrange{appendix:b}{appendix:bslut}


The problem is that I get the wrong page number for the last page -- it adds a page. What should be "pages 1-5" is instead "pages 1-6". I have Googled this like a madman, and I can't figure out what to do. I hope some of the superstars here can help! I have cleveref after hyperref, as recommended...

EDIT: As requested, I tried to make an MWE, but for some reason it gives me:

LaTeX Warning: Reference appendix:bslut' on page 1 undefined on input line 7.


Here it is, though. "multipage" should be a PDF spanning several pages.

\documentclass{memoir}
\usepackage{pdfpages}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\chapter{Foo}
Foo\cpagerefrange{appendix:b}{appendix:bslut}.

\chapter{Bar}
\label{appendix:b}
\includepdf[scale=0.8,pages=1,pagecommand=\chapter{Multipage}]{multipage}
\includepdf[scale=0.8,pages=2-,pagecommand={}]{multipage}
\label{appendix:bslut}

\end{document}

• labelling only works correctly,if there is \refstepcounter format done before. You need a counter for your includepdf, maybe a \begin{figure}...\end{figure} environment or something else. Please provide a small MWE – user31729 Jun 1 '14 at 10:03
• Thanks, I tried to do that. This is my first foray into labeling, and (as usual) it's much more complicated than it should be... – Kristian Nordestgaard Jun 1 '14 at 10:43
• You need to tell us what myref.sty is. – Andrew Swann Jun 1 '14 at 11:26
• Do you only want to make pagereferences or shall the included pdfs also be number, e.g. Interview 1.1, Interview 1.2 etc? – user31729 Jun 1 '14 at 11:54
• You could put the labels in the pagecommand: pagecommand=\chapter{Multipage}\label{appendix:b}. (Naturally this means that you will have to use a special \includepdf for the last page). – Ulrike Fischer Jun 1 '14 at 16:53

The following code gets the chapter title on the first page of the included PDF and enables the correct page references. mypages is a 16 page PDF used merely for demonstration purposes.

\documentclass{memoir}
\usepackage{pdfpages}
\usepackage{hyperref}
\usepackage{cleveref}

\makeatletter
\newcommand\incpdfchapter{%
\ifartopt\par\@nameuse{chapterblock}\else
\thispagestyle{chapter}
\global\@topnum\z@
\fi
\m@mindentafterchapter
\@ifstar{\@m@mschapter}{\@m@mchapter}}
\makeatother

\begin{document}
\chapter{Foo}
Foo\cpagerefrange{appendix:b}{appendix:bslut}.

\chapter{Bar}
\clearforchapter
\includepdf[scale=0.8,pages=1,pagecommand=\incpdfchapter{Multipage}\label{appendix:b}]{mypages}
\includepdf[scale=0.8,pages=2-15,pagecommand={}]{mypages}
\includepdf[scale=0.8,pages=16,pagecommand={}\label{appendix:bslut}]{mypages}

\end{document}


If you would prefer a more automated solution at the cost of a bit less flexibility in specifying your labels, you might try something like this:

\documentclass{memoir}
\usepackage{pdfpages}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{xparse}

\makeatletter
\newcommand\incpdfchapter{%
\ifartopt\par\@nameuse{chapterblock}\else
\thispagestyle{chapter}
\global\@topnum\z@
\fi
\m@mindentafterchapter
\@ifstar{\@m@mschapter}{\@m@mchapter}}
\newcounter{incpdfpages}
\NewDocumentCommand\myincpdf{ d<> O {} m m }{%
\clearforchapter
\IfNoValueTF{#1}{%
\def\incpdflabel{#3}}{%
\def\incpdflabel{#1}}
\includepdf[#2, pages=1, pagecommand=\incpdfchapter{#3}\label{\incpdflabel}]{#4}%
\setcounter{incpdfpages}{1}%
\includepdf[#2, pages=2-, pagecommand=\refstepcounter{incpdfpages}\label{\incpdflabel:\theincpdfpages}]{#4}%
}
\NewDocumentCommand\mysingpdf{ d<> O {} m m }{%
\clearforchapter
\IfNoValueTF{#1}{%
\def\incpdflabel{#3}}{%
\def\incpdflabel{#1}}
\includepdf[#2, pages=1, pagecommand=\incpdfchapter{#3}\label{\incpdflabel}]{#4}%
}
\makeatother

\begin{document}
\chapter{Foo}
Foo\cpagerefrange{appendix:b}{appendix:b:16}. Here is a reference to \cpageref{appendix:c}.

\chapter{Bar}
\myincpdf<appendix:b>[scale=0.8]{My Pages}{mypages}
\mysingpdf<appendix:c>[scale=0.8]{My Other Page}{mygraphics}

\end{document}


Again, mypages.pdf is assumed to be a 16 page PDF. The command \myincpdf<>[]{}{} takes 2 optional and 2 mandatory arguments.

1. The first optional argument is specified using <> and should be the label you'd like to use for the first page of the included PDF. Labels for later pages are constructed from this label by appending :n where n is the number of the page in the original PDF. For example, if the PDF has 16 pages, the last page gets the label baselabel:16 where baselabel is the label you specified for the first page. If you do not specify the label, it will be set to the value of the chapter title. (Be careful that you do not leave the label unset if the chapter title includes characters which don't work in labels.)
2. The second optional argument is specified using []. Include any extra options you want passed to \includepdf here.
3. The first mandatory argument should specify the title of the chapter.
4. The second mandatory argument should give the PDF file to be included.

Obviously, this is not really tested. I checked it worked for my example and for cases where I omitted the optional arguments but that's all. Caveat emptor.

EDIT: I've updated the code in response to the question concerning single-page PDFs in the comments. \mysingpdf is just like \myincpdf but it will include only the first page of the PDF. Use this command for single-page documents or for cases where you only want the first page to be included.

• Wow, this seems to work. Amazing! Bonus question, how should I deal with a single page PDF, using the second option? – Kristian Nordestgaard Jun 1 '14 at 22:56
• @KristianNordestgaard See updated answer. Basically, I'd use a separate command for that case which I've defined above as \mysingpdf`. Hope this helps. – cfr Jun 1 '14 at 23:11