3

In LuaLaTeX I am using \includepdf[pages=-, noautoscale=true]{myEmbeddedDoc.pdf} to include an external multi-page PDF file "myEmbeddedDoc.pdf". (This command is provided by the "pdfpages" package, \usepackage{pdfpages}.) Prior to this included PDF document, I would like to have text in my LaTeX document saying "See embedded document 'myEmbeddedDoc.pdf' on pages X-Y (N pages)" where X is the beginning page of the included document, Y is the ending page of the included document, and N is the number of pages of the included document. I would like X, Y, and N to be automatically populated by LaTeX. Is this possible? If so, how do I do it? (Do I need a different command or LaTeX package to include the external PDF document in order to accomplish this?)

Edit: Clarifying my question: I want the page numbers of the included PDF document within -- i.e., in the context of -- the including LaTeX document, not to parse any page numbers from the included document. For example, if myEmbeddedDoc.pdf (the included PDF document) is itself 3 pages long and gets included after page 4 of the LaTeX document -- meaning the included PDF document would comprise pages 5-7 of the LaTeX document -- then I want the text on page 4 of my LaTeX document to say "See myEmbeddedDoc.pdf on pages 5-7 (3 pages)." I need these numbers to be produced automatically, because this is being done many, many times in my LaTeX document. Manually editing the resulting PDF file won't cut it.

closed as unclear what you're asking by ChrisS, user13907, Maarten Dhondt, Stefan Pinnow, user31729 Jan 6 '17 at 23:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • For a general PDF document (not generated by LaTeX) it would be easier just to use a PDF viewer with search capabilities and copy the information by hand. With the right editor you can read a PDF document as ASCII and find the anchor names (if any). If the PDF was/is created by LaTeX you can use the aux or index files directly (possibly using \includeonly). – John Kormylo Jan 2 '17 at 3:50
  • Welcome! Please provide a proper minimal example and clarify the question. Do you mean the page numbers in the document doing the including or some page numbers in the included PDF? Getting a count of the pages in the PDF is easy since you are including the lot. But the numbers which happen to be printed on those pages are a different matter and may or may not be possible at all. Impossible to say on the basis of the information currently provided. – cfr Jan 2 '17 at 4:17
4

Assume lipsum50.pdf contains 50 paragraphs of Lorem Ipsum text (generated by \lipsum[1-50] thanks to lipsum.

The following minimal example provides \insertpdf[<opts>]{<PDF>} which works just like \includepdf[<opts>]{<PDF>} with the addition of adding a *-firstpage and *-lastpage \label that you can \pageref. Additionally, \pagediff{<PDF>} uses the above page references to calculate the number of pages included*.

enter image description here

\documentclass{article}

\usepackage{pdfpages,refcount}
\usepackage{lipsum}

\makeatletter
\newcommand{\insertpdf}[2][]{%
  \clearpage
  \label{#2-firstpage}%
  \includepdf[#1]{#2}%
  \immediate\write\@auxout{\string\newlabel{#2-lastpage}{{\@currentlabel}{\number\numexpr\value{page}-1}}}%
}
\newcommand{\pagediff}[1]{%
  \number\numexpr\getpagerefnumber{#1-lastpage}-\getpagerefnumber{#1-firstpage}+1\relax
}
\makeatother

\begin{document}

\section{A section}
\lipsum[1-30]% Pages 1-6

See pages \pageref{lipsum50-firstpage}-\pageref{lipsum50-lastpage} (\pagediff{lipsum50} pages).

\insertpdf[pages=1-3]{lipsum50}% Pages 7-9

\lipsum[31-50]% Pages 10-13

\end{document}

* There are also other ways of calculating the number of pages in an external PDF. See Get number of pages of external PDF.


A hyperref-compatible solution requires a slight modification. The first \pagereference can be used as-is since the \label is set on the first page of inclusion. The last page is actually calculated and therefore \pageref cannot be used directly. Instead, we point directly to the page using the page.X \hyperlink (see PDF hyperlinks to a given page).

\documentclass{article}

\usepackage{pdfpages,refcount,hyperref}
\usepackage{lipsum}

\makeatletter
\newcommand{\insertpdf}[2][]{%
  \clearpage
  \phantomsection
  \label{#2-firstpage}%
  \includepdf[#1]{#2}%
  \immediate\write\@auxout{\string\newlabel{#2-lastpage}{{\@currentlabel}{\number\numexpr\value{page}-1}{}{}{}}}%
}
\newcommand{\pagediff}[1]{%
  \number\numexpr\getpagerefnumber{#1-lastpage}-\getpagerefnumber{#1-firstpage}+1\relax
}
\makeatother

\begin{document}

\section{A section}
\lipsum[1-30]% Pages 1-6

See pages \pageref{lipsum50-firstpage}-%
  \hyperlink{page.\getpagerefnumber{lipsum50-lastpage}}{\pageref*{lipsum50-lastpage}} (\pagediff{lipsum50} pages).

\insertpdf[pages=1-3]{lipsum50}% Pages 7-9

\lipsum[31-50]% Pages 10-13

\end{document}
  • Thanks, but your example breaks when I'm using the package "hyperref" in the including document. How do you make it work with "hyperref"? – user553702 Jan 3 '17 at 1:14
  • @user553702: Of course; hyperref completely changes how labels and references work... for future reference, please include all details relevant to your problem in your question. To that end, also include a minimal working example (MWE), m'kay? – Werner Jan 3 '17 at 1:19
  • Sorry about that; it was difficult to foresee that that would be a factor. In any case, can we proceed using the example that you just posted? I'm able to work with that for the time being. Do you know how it can be changed so that it works with hyperref? – user553702 Jan 3 '17 at 1:25
  • @user553702: I've added an option that works with hyperref. I'm sure there are other solutions as well. – Werner Jan 4 '17 at 1:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.