Consider the following MWE:
\documentclass{article}
\DeclareKeys [ myKeys ]{
a .store = \whereA,
b .store = \whereB,
}
\SetKeys [ myKeys ] {
a = AAA,
b = \pageref{#2},
}
\NewDocumentCommand{\abref}{O{} m}{%
\SetKeys [ myKeys ] {#1}%
\whereA\ and \whereB%
}
\begin{document}
\section{Some topic}
Ref the other page \abref{newpage} % does NOT work
\vspace{2em}
Ref the other page \abref[b = \pageref{newpage}]{newpage} % works
\newpage
foobar \label{newpage}
\end{document}
It produces the following output:
The problem lies in the fact the default value for parameter b, viz. \pageref{#2}, gets evaluated too early.(*) I have a workaround for this: use a placeholder instead (<pnum> shall we say), and replace it inside the command definition (requires xstring):
\IfSubStr{\whereB}{<pnum>}%
{\StrSubstitute{\whereB}{<pnum>}{\pageref{#2}}}%
But my question is if there is some way to prevent that early evaluation, thus making the workaround redundant. I have tried, e.g., \noexpand and some such incantations, but with no luck.
Thank you in advance for your help.
(*) In fact it produces the following warning:
LaTeX Warning: Reference `##2' on page 1 undefined on input line 24.