[Tex/LaTex] get the page number of included PDF page

pdfpages

I have to put some academic papers at the end of a document, but only include a subset of their pages (due to the page limitations in our project). Now, I would like to mark each included page with the original page number of the PDF paper I'm including.

Here is my MWE to illustrate the question.

\documentclass{article}
\usepackage{pdfpages}

\begin{document}

\includepdf[pages={1-2,4},picturecommand={\put(30,30){\makebox{Lorem Ipsum p. XX}}}]{lorem.pdf}

\end{document}

I created a second PDF called lorem.pdf by just typesetting:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\lipsum[1-25]
\end{document}

which creates a 4-page document lorem.pdf. Now, when I include only parts of the paper, namely pages 1, 2, and 4, I would like to fill in these numbers in the 'picturecommand' text instead of XX. Is there a way to reference that number there?

Best Answer

Assuming that your input document (lorem.pdf) has no pages numbers and that the page numbers specified by the key-value pages={1-2,4} are the actual page numbers in the source document, the macro \AM@page contains the value you're after:

\documentclass{article}
\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
\begin{document}
\makeatletter
\includepdf[
   pages={1-2,4},
   picturecommand={\put(30,30){\makebox{Lorem Ipsum p.\ \AM@page}}}]
  {lorem.pdf}
\makeatother
\end{document}

The above MWE prints

Lorem Ipsum p. 1
Lorem Ipsum p. 2
Lorem Ipsum p. 4

in the lower-left corner.

If the above conditions are not met, then it would be possible to temporarily modify \AM@page to be a certain shift from the given values.