2

I have tried this to use the number of pages as an option to a package:

\documentclass{article}
\usepackage{lipsum}
\usepackage{lastpage}
\usepackage{refcount}
\newcounter{mylastpage}
\setcounterpageref{mylastpage}{LastPage}
%\usepackage[\themylastpage]{package}
\begin{document}
    \themylastpage
    \setcounterpageref{mylastpage}{LastPage}
    \themylastpage
    \lipsum[1-100]
\end{document}

However, \setcounterpageref seem to work only in the document, not before. What else can I do? What I am trying to achieve is this:

\hypersetup{pdfprintpagerange=2 \themylastpage}

This shall skip printing the first two pages of my document by default; I know I have to adapt \themylastpage to account for numbering style or 0-based counting, but first things first...

  • Note that the setting of the printing page range will most likely only be usable inside Adobe Acrobat/Reader. – Werner Feb 1 '16 at 21:19
  • That's acceptable :) – bers Feb 1 '16 at 21:21
3

You could write the number of pages to a file at the very end of the document. That way you can read it in during your pages load (if it exists) and set content accordingly:

\documentclass{article}
\usepackage{lipsum}
\usepackage{atveryend}
\usepackage{hyperref}

\AtVeryEndDocument{%
  \newwrite\pagesfile% Create new write file
  \immediate\openout\pagesfile=\jobname.pages
  \immediate\write\pagesfile{% Write...
    \noexpand\newcounter{totalpages}% ...counter creation and...
    \noexpand\setcounter{totalpages}{\number\numexpr\value{page}-2}}% ...number of pages setting
  \immediate\closeout\pagesfile% Close file
}

% If the file already exists, read it in and set pdfprintpagerange
\InputIfFileExists{\jobname.pages}
  {\hypersetup{pdfprintpagerange=2 \thetotalpages}}
  {}

\begin{document}
Total pages: \ifcsname thetotalpages\endcsname
  \number\numexpr\value{totalpages}+1\relax
\fi

\sloppy% Just for this example
\lipsum[1-100]
\end{document}

The file \jobname.pages in the above creation should contain

\newcounter {totalpages} \setcounter {totalpages}{18}

Some things to note here:

  1. The page counter is different to other counters in that it is initialized to 1.

  2. Adobe uses a zero-based counter for the pages. This, together with (1) above makes for the need to store the value of the page counter minus 2. That's also why I add one to the displaying the page number inside the document itself (purely for aesthetics).

  3. Many PDF document property settings only work for Adobe-related products.

| improve this answer | |
  • I think your calculation is off by one. The pdf says Total pages: 18, but the file has 19 pages. Otherwise, nice solution. It's probably what totcount was made for (for lazy people like me). – bers Feb 2 '16 at 3:00
  • @bers: That's true. I forgot to add one to the display. But for the pdfprintpagerange property it works as you're using a zero-based page enumeration and writing after the last shipout, so the page counter has been incremented once more. I've updated the display part of the minimal example. – Werner Feb 2 '16 at 3:01
1

This finally works:

\documentclass{article}
\usepackage{hyperref}
\usepackage{lipsum}

% this is the solution
\usepackage{lastpage,refcount,totcount}
\newtotcounter{totlastpage}
\hypersetup{pdfprintpagerange=2 \the\totvalue{totlastpage}}
\AtBeginDocument{
    \setcounterpageref{totlastpage}{LastPage}
    \addtocounter{totlastpage}{-1}
}

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

Is this a good way of doing it?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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