0

Using the patch defining prelast for throws an error. [Might it have to do with updates in pdfpages?]

I am using the second answer from Including a length-dependent page range using pdfpages by @Werner and edited by @DavidCarlisle.

Using the command \includepdf[pages=1-3]{dummy-l.pdf} everything works fine. But when I try \includepdf[pages=1-prelast]{dummy-l.pdf} instead, I receive the error:

Package pdfpages Error: Cannot find file `1-prelast'.

The goal is to read in pdfs of different lengths and color the last page of a text. When I removed \usepackage{xcolor} and all the \pagecolor{} commands, I still got the same error. However, since the color part is critical to my project I have left it in the code just in case.

Note that once this error has been fixed, I will have other questions from this project, but I didn't want to add them here.

\documentclass{amsart}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
    \pagecolor{white}
\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEFINE “prelast” to contain the pagenumber of the next-to-last page
% in pdfpages — from answer by Werner and David Carlisle found at
% https://tex.stackexchange.com/questions/62911/including-a-length-dependent-page-range-using-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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\pagecolor{cyan!50!white} Some Text \clearpage % before changing color must be on next page

\pagecolor{white} \includepdf[pages=1-prelast]{dummy-l.pdf} % <----- error % can't find file `1-prelast'. %\includepdf[pages=1-3]{dummy-l.pdf} <----- works \pagecolor{pink} \includepdf[pages=last]{dummy-l.pdf} % <----- works %\includepdf[pages=4]{dummy-l.pdf} <----- works \pagecolor{white}

Some more text.

\end{document}

Bryan H-M
  • 397
  • 1
  • 11

1 Answers1

2

I wouldn't patch pdfpages like this (and certainly not with code from a 10 years old answer). I would retrieve the page number as in the other answer, or in a more modern version.

Side remark: the usenames option is obsolete and will be removed in some future version.

\documentclass{amsart}
\usepackage{l3graphics}
\usepackage[dvipsnames,svgnames]{xcolor}
    \pagecolor{white}
\usepackage{pdfpages}% http://ctan.org/pkg/pdfpages
\ExplSyntaxOn
\newcommand\prelast{}
\newcommand\getprelast[1]
  {
   \graphics_get_pagecount:nN{#1}\l_tmpa_tl 
   \tl_set:Nx\prelast{\int_eval:n{\l_tmpa_tl-1}}
  }
\ExplSyntaxOff

\begin{document}

\pagecolor{cyan!50!white} Some Text \clearpage % before changing color must be on next page

\pagecolor{white} \getprelast{example-image-a4-numbered.pdf} \includepdf[pages=1-\prelast]{example-image-a4-numbered} \pagecolor{pink} \includepdf[pages=last]{example-image-a4-numbered}
\pagecolor{white}

Some more text.

\end{document}

Ulrike Fischer
  • 327,261
  • Thank you @Ulrike. I'll wait another day before accepting an answer, but in the mean time I will work from your solution. I'm assuming that the LaTeX L3 extensions will work equally well in XeLaTex? I've started going through some of the documentation, and believe that it will also offer solutions to most of the other parts of the project I was worried about. If I need help with it I'll post a different question. – Bryan H-M Aug 11 '22 at 22:40