This is a slightly simplified version of a macro I've been writing (using pgfkeys and pdfpages) to include PDF files.

This seems to be working.

I ran into one bump. includepdf does not like it when I try to pass a macro as a argument to the pages argument. I implemented a workaround I found in Werner's answer to the question "how to expand a keyval argument", which is about the same issue. I left out Werner's usage of groups, which I thought was unnecessary, and made things more complicated. I initially thought that I could use the pgfkeys .expand once handler to work around this, but this doesn't seem to be possible outside PGF/TikZ. For some magical reason this seems to be possible inside PGF/TikZ, I don't know why.

  1. I'm not really familiar with pgfkeys or any TeX key-value package, for that matter. So suggestions for improvements with respect to my pgfkeys usage would be much appreciated.
  2. Without the workaround implemented (i.e. passing a macro directly to the pages argument of includepdf) I get the same error as Vitomir Kovanovic does in that question, namely:

    ERROR: Missing = inserted for \ifnum.

    but this only happens when - or , is passed as part of the argument to pages, not otherwise. I'm just wondering why this error only occurs in that case. Is there any simple reason why?

  3. Also, is there any other or better workaround for this expansion issue than the one I used? In particular, does pgfkeys offer any workarounds using the .expand once handler, or otherwise?
  4. This one is a bit more vague, but why does the .expand once handler even work in PGF/TikZ? It somehow expands the argument before passing that, but how does it manage to do that when regular TeX can't do it? I'm very unclear what is going on.

  /pdffile/.is family, /pdffile,
  % Calling the keys so that the macros are still defined if 
  % arguments are not passed.
  default/.style =
  captioning/.is if=hascaptioning,
  blankpage/.is if=hasblankpage,
  cappos/.default = {(7,-4)},
  pagenum/.default = {1-},
  cappos/.estore in = \cappos,
  cap/.estore in = \cap,
  pagenum/.estore in = \pagenum,

% #1 optional argument, used to pass list of keys from pgfkeys
% #2 filename
\NewDocumentCommand{\pdffile}{o m}
  \pgfkeys{/pdffile, cap/.default = #2}
  \pgfkeys{/pdffile, default, #1}%

      \unexpanded{\begin{tikzpicture}[remember picture, overlay]
          % Add caption
          \node [font=\bfseries, align=center] at \cappos{\cap};
  % Add blank page
  \clearpage \phantom{} \clearpage


\pdffile[captioning, cap=FOOBAR, cappos={(7,1)}, pagenum=2-3]{foo.pdf}

  • Your example is to complicated. Reduce it to the problem. Jul 17, 2019 at 8:38
  • \catcode_=12` is not only a bad idea, but you don't even make use of it. Jul 17, 2019 at 8:39
  • @HenriMenke I use it, just not in this example. I have names (usually files) with underscores, and it's a nuisance dealing with that. I wasn't planning on including any maths in here, and egreg wrote an answer saying he thought it was harmless if one wasn't going to doing subscripting. See tex.stackexchange.com/a/359804/3406. Was there some other reason I don't know about? In any case, I've now removed it from the example. Jul 17, 2019 at 8:42
  • @FaheemMitha pgfkeys uses \scantokens in some places and when you set a key containing _ with catcode 12 but the catcode is different when fetching the key, everything will blow up. Jul 17, 2019 at 8:47
  • @HenriMenke I see. Very interesting. In that case, please do write an answer to tex.stackexchange.com/q/359787/3406, because that isn't generally known. Is the underscore page safe for this pgfkeys usage, then? Jul 17, 2019 at 8:49

1 Answer 1


Well if I got it right the core problem is that \includepdf doesn't handle pages=\mypages well if \mypages is a range. Imho the correct solution to this problem is to write the author of pdfpages and ask him that he adds the necessary code to expand the value once. E.g. along this lines:




Alternatively I would define a new pdfpages key, e.g. pageso which does this expansion. I would not use the \edef\next method here, that looks overcomplicated.

  • "Alternatively I would define a new pdfpages key, e.g. pageso which does this expansion." Could that be user-defined, using pgfkeys or otherwise, or would it need to be part of pdfpages? If the former, an illustrative example would be much appreciated. Jul 17, 2019 at 9:59
  • 1
    Simply take the definekey above and replace pages by pageso. Jul 17, 2019 at 10:22
  • Interesting approach, but this would approach would require understanding each package well enough to make such changes. In any case, changing pages to pageso in the definekey did not work for me. Jul 17, 2019 at 19:21
  • well you then naturally should also use the new key. But imho the best is to write the author that he expands. Jul 17, 2019 at 19:38
  • Yes, I did use the new key as well. I changed your code in three places. Namely, the \define@key line, the \AM@CheckValue line, and in the argument to \includepdf. I also tried different variations. I wasn't sure about what the \AM@CheckValue line was for, but it doesn't work even if I don't; change that line. See if it works for you. I did write to the author already, asking him to add that feature. Jul 18, 2019 at 5:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .