Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to extract pages from 16 to (last page minus one) using pdfpages. I know I can access the last page via the token last, but I need to do arithmetic on it, to get a page range that looks like 16-(last-1).

Is there a way to do this in pdfpages ?

share|improve this question
1  
Related but not a duplicate: How to know the number of pages in a PDF file?. –  Fifa Earth Cup 2014 Jul 11 '12 at 11:24
add comment

2 Answers

up vote 6 down vote accepted

Assuming you use pdflatex to TeXify the following,

\documentclass{article}
\usepackage{pdfpages}


\begin{document}
\pdfximage{pstricks-doc.pdf}%
\newcount\N
\N=\pdflastximagepages\relax
\advance\N by -1\relax
\includepdf[pages=16-\the\N]{pstricks-doc}
\end{document}

For XeLaTeX (and PDFLaTeX) fans,

\documentclass{article}

\usepackage{pdfpages}
\usepackage{ifxetex}

\ifxetex
    \newcount\pdflastximagepages
    \def\pdfximage#1{\pdflastximagepages=\XeTeXpdfpagecount"#1"\relax}
\fi

\begin{document}
\pdfximage{filename.pdf}%
\newcount\N
\N=\pdflastximagepages\relax
\advance\N by -1\relax
\includepdf[pages=16-\the\N]{filename}
\end{document}
share|improve this answer
add comment

The following redefinition of \AM@checklast allows you to now use the new key prelast to insert up to the before-last page:

\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
\makeatletter
\def\AM@checklast#1-\END{%
  \edef\AM@tempii{\AM@trim@space{#1}}%
  \expandafter\ifx\expandafter\\\AM@tempii\\
    \@tempcntb=\AM@pagecount\relax
    \ifAM@DVIoutput
      \ifnum\AM@pagecount=1
        \@tempcntb\@tempcnta
      \fi
    \fi
    \advance\@tempcntb\@ne
    \whiledo{\@tempcnta<\@tempcntb}{%
      \AM@append{\the\@tempcnta}%
      \advance\@tempcnta\@ne
    }%
  \else
    \def\AM@cmp{last}%
    \ifx\AM@cmp\AM@tempii
      \let\AM@tempii\AM@pagecount
    \else\def\AM@cmp{prelast}% <------------------------- ADDED
      \ifx\AM@cmp\AM@tempii%                                  |
        \@tempcntb=\numexpr\number\AM@pagecount-1\relax%      |
        \edef\AM@tempii{\the\@tempcntb}%                      |
      \fi% <---------------------------------------------------
    \fi
    \AM@checkinteger{\AM@tempii}%
    \ifAM@integer
      \@tempcntb=\AM@tempii\relax
      \AM@checkpagenumber{\the\@tempcntb}%
      \ifnum\@tempcnta<\@tempcntb \def\AM@rel{<}\def\AM@inc{\@ne}%
      \else \def\AM@rel{>}\def\AM@inc{\m@ne}\fi
      \advance\@tempcntb\AM@inc
      \whiledo{\@tempcnta\AM@rel\@tempcntb}{%
        \AM@append{\the\@tempcnta}%
        \advance\@tempcnta\AM@inc
      }%
    \else
      \AM@setnewdocname{\AM@temp}%
    \fi
  \fi
}
\makeatother

The idea behind it is to insert a test case for the prelast "page", and to subtract one from \AM@pagecount - the number of pages within the document being read.

The intended use would be

\includepdf[pages=16-prelast]{file}
share|improve this answer
    
Your solution is quite impressive, and does much more than what I had originally asked. But for what I need, the other answer suffices, and so I accepted it. –  Suresh Jul 11 '12 at 15:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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