4

Consider the \includepdf macro from the pdfpages package. If I do

\includepdf[pages=1-]{blank.pdf}

there is no problem. However, if I do

\def\foo{{1-}}
\includepdf[pages=\foo]{blank.pdf}

I get the error

! Missing = inserted for \ifnum.

I earlier thought that the problem was that \includepdf wasn't expanding the macro argument to pages, but now I'm wondering if that is indeed the case. Because if I try

\includepdf[pages=\expanded{\foo}]{blank.pdf}

it makes no visible difference.

I've tried running with \tracingmacros=1 and it looks like in both cases \foo at least gets expanded, but it's unclear what goes wrong after that. MWE follows.

\documentclass{article}
\usepackage{grffile}
\usepackage{pdfpages}

\begin{document}
\tracingmacros=1
\def\foo{{1-}}
\includepdf[pages=\foo]{blank.pdf}
% \includepdf[pages=\expanded{\foo}]{blank.pdf}
\tracingmacros=0
\end{document}

And here is part of the \tracingmacros=1 output corresponding to the version with \expanded, namely

\includepdf[pages=\foo]{blank.pdf}

Output follows:

\foo ->{1-}

\AM@trim@spacei #1->\AM@trim@spaceii #1 \END 
#1<-{1-}

\AM@trim@spaceii #1 #2\END ->#1
#1<-1-
#2<-

\AM@checkinteger #1->\ifcat _\ifnum 9<1#1_\else A\fi \AM@integertrue \else \AM@
integerfalse \fi 
#1<-\AM@tempi 

\AM@tempi ->1-

\AM@integertrue ->\let \ifAM@integer \iftrue 

\AM@checkpagenumber #1->\ifnum #1>\AM@pagecount \relax \ifthenelse {\boolean {A
M@pkg@draft} \and \boolean {AM@survey}}{}{\PackageError {pdfpages} {Page #1 of 
`\AM@currentdocname ' does not exist} {You have set a wrong page number in the 
`pages' option. Document \MessageBreak `\AM@currentdocname ' does not have #1 p
ages.}}\fi 
#1<-\AM@tempi 

\AM@tempi ->1-

! Missing = inserted for \ifnum.
<to be read again> 

As can be seen, \foo does get expanded in this case.

Related question earlier asked by me: "Passing arguments using pgfkeys to a macro for including PDF files, using pdfpages".

  • Not tested, but the problem probably is due to the extra set of braces. Possibly in an early stage of processing, the package tries to search for a -, which it doesn't find because it's hidden between {...}. Then when checking the value of the page it assumes there is no - and tries to do \ifnum 1->\AM@pagecount, which is invalid. Probably if you remove the extra set of braces it will work. – Phelype Oleinik Sep 16 '19 at 13:13
  • 1
    Values to keys are generally not expanded; the macros for detecting a range require the hyphen to be “visible” to begin with. – egreg Sep 16 '19 at 13:13
  • @PhelypeOleinik It does the same thing without the braces. I could remove the braces if you want. – Faheem Mitha Sep 16 '19 at 13:15
  • @egreg What do you mean by a "visible" hyphen? And if the issue is that the keys are not expanded, why doesn't \expanded work? Am I applying it incorrectly? – Faheem Mitha Sep 16 '19 at 13:16
  • @FaheemMitha Because values are not expanded, so instead of \foo the macros for detecting a range see \expanded{\foo} which has no hyphen as well. – egreg Sep 16 '19 at 13:17
5

When pdfpages examines the pages key, it does what's specified in its definition

\define@key{pdfpages}{pages}{\AM@CheckValue{pages}{#1}%
                             \def\AM@pagestemp{#1}}

The \AM@CheckValue part is not a problem, because of

 \def\AM@CheckValue#1#2{%
  \ifx\\#2\\\PackageError{pdfpages}
             {Option `#1' must have a non-empty value}%
  \fi
}

(well, \PackageError should have three arguments, so it's best not passing an empty value).

Then \AM@pagestemp is set to contain the value, in your case \foo; it is initialized to contain 1.

After that, \expandafter\AM@readlist\expandafter{\AM@pagestemp} is called, which becomes

\AM@readlist{\foo}

This is a macro that splits the argument at commas and each item is passed to \AM@checkrange by

\AM@checkrange#1-\END

In your case, #1 is \foo. The macro is defined by

\def\AM@checkrange#1-#2\END{...}

and #2 being empty means that no hyphen is present in the current item. But the macro also does

\edef\AM@tempi{\AM@trim@space{#1}}

so, in your case, \AM@tempi gets to contain 1-; however, the expected value is an integer, which 1- isn't. Error.

You can add to your document

\makeatletter
\define@key{pdfpages}{pages}{\AM@CheckValue{pages}{#1}%
                             \edef\AM@pagestemp{#1}}
\makeatother

but don't blame me if something goes wrong.

  • Thank you for the detailed explanation. But shouldn't this just work? – Faheem Mitha Sep 16 '19 at 14:38
  • @FaheemMitha No, why? The package author decided not to support macros in the value of the key pages. – egreg Sep 16 '19 at 14:59

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.