0

Background: I want a way to obtain the number of pages of the PDF that is included using \includepdf (from the pdfpages package). To be clear, I mean the PDF consisting of the (possibly proper) subset of the pages of the original PDF that is passed as an argument to \includepdf. E.g. if a 10 page document is an argument to \includepdf, and only three pages are selected, I want the value three.

The reason for this, is that I want to programmatically add a blank page at the end of an document with an odd number of pages, but not to the end of a document with an even number of pages. So I technically only need to know whether the number is even or odd, not the actual number.

See MWE below. I have called the hypothetical macro that contains the number of pages \numberofpages.

Since TeX has effectively global scope, I'm assuming that the macro will return the number of pages corresponding to the most recent invocation of \includepdf.

\documentclass[12pt]{article}
\usepackage{pdfpages}
\def\Filename{example-image-a4-numbered.pdf}
\def\numberofpages{}
\begin{document}
\includepdf[pages={2-4, 6}]{\Filename}
\numberofpages % 4
\includepdf[pages={4-6}]{\Filename}
\numberofpages % 3
Faheem Mitha
  • 7,778
  • probably pdfpages has this number but don't you know this in advance, you know the first is 4 and the second is 2 (or there is an error) – David Carlisle Feb 05 '21 at 12:03
  • 2
    why do you need to know the number of pages included? don't you just want to know whether the current page number after the inclusion is even or odd? (\ifodd\count0 ...` – David Carlisle Feb 05 '21 at 12:07
  • 1
    Isn't something like \clearpage\ifodd\value{page}\else\null\clearpage\fi enough? – campa Feb 05 '21 at 12:08
  • @DavidCarlisle True, the current page number after the inclusion would work too. – Faheem Mitha Feb 05 '21 at 13:30
  • @campa Not sure what you mean. Can you elaborate? Or just produce a complete example? – Faheem Mitha Feb 05 '21 at 13:31
  • That line of code will conditionally add a blank page at the end of the document such that the document contains an even number of pages. – campa Feb 05 '21 at 13:34
  • @campa Well, I need it after every invocation of \includepdf, to optionally add a blank page after each inclusion. Would that work? – Faheem Mitha Feb 05 '21 at 13:35
  • but the current page number is just the current page number the fact that you use \includpdf on a previous page is not relevant. – David Carlisle Feb 05 '21 at 14:13
  • 2
    I suggest this is a duplicate of https://tex.stackexchange.com/a/248829/1090 (and others) – David Carlisle Feb 05 '21 at 14:19

0 Answers0