Including a length-dependent page range using pdfpages

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}

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}

Tags:

Pdfpages