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.
- I'm not really familiar with
pgfkeysor any TeX key-value package, for that matter. So suggestions for improvements with respect to mypgfkeysusage would be much appreciated. Without the workaround implemented (i.e. passing a macro directly to the
pagesargument ofincludepdf) 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 topages, not otherwise. I'm just wondering why this error only occurs in that case. Is there any simple reason why?- Also, is there any other or better workaround for this expansion
issue than the one I used? In particular, does
pgfkeysoffer any workarounds using the.expand oncehandler, or otherwise? - This one is a bit more vague, but why does the
.expand oncehandler 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.
\documentclass[12pt]{scrartcl}
\usepackage{grffile}
\usepackage{pdfpages}
\usepackage{pgfkeys}
\usepackage{xparse}
\usepackage{tikz}
\pgfkeys
{
/pdffile/.is family, /pdffile,
% Calling the keys so that the macros are still defined if
% arguments are not passed.
default/.style =
{
captioning=false,
blankpage=false,
cappos,
cap,
pagenum
},
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}
{
\newif\ifhasgrid
\newif\ifhascaptioning
\newif\ifhasblankpage
\pgfkeys{/pdffile, cap/.default = #2}
\pgfkeys{/pdffile, default, #1}%
\edef\tempx{\noexpand\includepdf[pagecommand=
{
\unexpanded{\begin{tikzpicture}[remember picture, overlay]
% Add caption
\ifhascaptioning
\node [font=\bfseries, align=center] at \cappos{\cap};
\else\fi
\end{tikzpicture}}
}
,pages=\pagenum]{#2}}
\tempx
% Add blank page
\ifhasblankpage
\clearpage \phantom{} \clearpage
\else\fi
}
\begin{document}
\pdffile[captioning, cap=FOOBAR, cappos={(7,1)}, pagenum=2-3]{foo.pdf}
\end{document}
\catcode_=12` is not only a bad idea, but you don't even make use of it. – Henri Menke Jul 17 '19 at 08:39pgfkeysuses\scantokensin 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. – Henri Menke Jul 17 '19 at 08:47underscorepage safe for thispgfkeysusage, then? – Faheem Mitha Jul 17 '19 at 08:49underscorepackage is actually worse, because it makes_active which is problematic not only inpgfkeysbut also everywhere else. – Henri Menke Jul 17 '19 at 08:50example-image-a4-numbered.pdf). – Ulrike Fischer Jul 17 '19 at 08:53pgfkeyshandles it. PGF experts, explanations would be much appreciated. – Faheem Mitha Jul 17 '19 at 08:55underscoredoesn't cut it either. People should be educated on the issues. – Faheem Mitha Jul 17 '19 at 09:02\edef\tempx, which is then called by\tempx. You can compare it to Werner's answer, it's the same idea. I could break this up further, but since the questions are all interrelated, I though it easier to just make one post. And again, I don't currently have a problem, but would like some explanations. And the example is relatively long because I'm using a number of differentpgfkeystechniques, and it takes some space. – Faheem Mitha Jul 17 '19 at 09:07