Cross-referencing to included PDF with counter and argument in the label

I would like to use the following code for including PDF documents in my LaTeX document:

\documentclass[12pt,a4paper,fleqn,parskip=half]{scrartcl}

\usepackage{lscape}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[scaled]{helvet}
\usepackage{caption}
\usepackage{xargs}

%---------------------------------------------------------------------
%   % Macro to find the number of pages of included PDF document
%---------------------------------------------------------------------
\makeatletter
\newcounter{imagepage}
\newcommand*{\foreachpage}[2]{
\begingroup
\sbox0{\includegraphics{#1}}
\xdef\foreachpage@num{\the\pdflastximagepages}
\endgroup
\setcounter{imagepage}{0}
\@whilenum\value{imagepage}<\foreachpage@num\do{
\stepcounter{imagepage}
#2\relax
}
}
\makeatother

%---------------------------------------------------------------------
%   % Macro to include PDF document with page numbers in caption and label
%---------------------------------------------------------------------
\makeatletter
\newcommandx{\picTEST}[5][1=0.9, 2=0]
{
\foreachpage{#4}
{
\begin{figure}[h!]
\centering
\makebox[\textwidth]{\includegraphics[width=#1\hsize,page=\value{imagepage},angle=#2]{#4}}
\label{#3\theimagepage}
\caption{#5 - Page \arabic{imagepage} of {\the\pdflastximagepages}}
\end{figure}
\clearpage
}
}
\makeatother

\begin{document}

\section{Some Text}
Here is reference to page \ref{Document1_Label_Page3} of the included PDF document.

\newpage
\section{Some insert of PDF document}
\picTEST{Document1_Label_Page}{Document1.pdf}{Document1}

\end{document}


The problem is that the label in the text does not expand as a number '3', but I get '??' instead. However, when I click on '??' in the text, the cross-referencing to the page number 3 of the included PDF document works well - it jumps to this page (or better say to this figure). I suppose the problem is in the second macro \picTEST where I use \label{#3\theimagepage} and maybe in the text where I am referencing \ref{Document1_Label_Page3}. I would very kindly ask for support. Please use some example multi page PDF document named Document1.pdf.

• Welcome to TeX.SX! It's difficult to test your code, as we don't have Document1.pdf etc. at hand.
– user31729
Oct 31, 2014 at 17:48
• There are two 'errors'... First: Use \label after \caption and you are referring to page 3 (as far as I understand it, but does your document have a page 3 at all?)
– user31729
Oct 31, 2014 at 17:59
• I wonder, if this feature could not be achieved more easily with pdfpages package?
– user31729
Oct 31, 2014 at 18:07
• pdfpages does not do the job in my case since, if I am not mistaken, it simply attaches the PDF document without keeping the defined layout (header, footer, etc.) of the main tex document. Nov 3, 2014 at 12:10

The \label command in the \picTEST macro must come after \caption, as it needs a \refstepcounter call, which is done within \caption. Otherwise the label points to something different or to nowhere.

Furthermore, the document must have the corresponding number of pages which should be referred to.

\documentclass[12pt,a4paper,fleqn,parskip=half]{scrartcl}

\usepackage{lscape}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[demo]{graphicx}
\usepackage[scaled]{helvet}
\usepackage{caption}
\usepackage{xargs}

%---------------------------------------------------------------------
%   % Macro to find the number of pages of included PDF document
%---------------------------------------------------------------------
\makeatletter
\newcounter{imagepage}
\newcommand*{\foreachpage}[2]{
\begingroup
\sbox0{\includegraphics{#1}}
\xdef\foreachpage@num{\the\pdflastximagepages}
\endgroup
\setcounter{imagepage}{0}
\@whilenum\value{imagepage}<\foreachpage@num\do{%
\stepcounter{imagepage}
#2\relax
}
}
\makeatother

%---------------------------------------------------------------------
%   % Macro to include PDF document with page numbers in caption and label
%---------------------------------------------------------------------
\makeatletter
\newcommandx{\picTEST}[5][1=0.9, 2=0]
{
\foreachpage{#4}
{
\begin{figure}[h!]
\centering
\makebox[\textwidth]{\includegraphics[width=#1\hsize,page=\value{imagepage},angle=#2]{#4}}
\caption{#5 - Page \arabic{imagepage} of {\the\pdflastximagepages}}
\label{#3\theimagepage}
\end{figure}
\clearpage
}
}
\makeatother

\begin{document}

\section{Some Text}
Here is reference to page \ref{Document1_Label_Page3} of the included PDF document.

\newpage
\section{Some insert of PDF document}
\picTEST{Document1_Label_Page}{quickcode.pdf}{Document1}

\end{document}


quickcode.tex

\documentclass{book}

\usepackage{forloop}

\newcounter{loopcounter}
\usepackage{blindtext}

\begin{document}
\forloop{loopcounter}{1}{\value{loopcounter} < 11}{%
\blindtext[\number\value{loopcounter}]
\newpage
}%

\end{document}


• Such a simple mistake... Well, devil in the details. Thank a lot for help. Nov 3, 2014 at 12:05