This is a follow-up to the question linked in the references section with the difference that the page information is coming from a macro. That is, instead of
\href[page=2]{foo.pdf}{page 2}
I would like to use
\def\PageNum{2}%
\def\HrefPageOption{page=\PageNum}%
\href[\HrefPageOption]{foo.pdf}{page \PageNum}
But, this results in
Package keyval Error: page=2 undefined.
I have tried my usual expansion hacks, but have not been able to get this to work. The expected output should have the complete last line:
Notes:
- The MWE below runs as is. To get the error condition, the last line needs to be uncommented.
- As TeXShop uses MacOS pdfkit, the links created don't seem to work (matching behavior of MacOS Preview). However with Skim or Acrobat the links do work.
References:
Code:
\documentclass{article}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{lipsum}
\begin{filecontents}{foo.tex}
\documentclass{article}
\usepackage{mathtools}
\usepackage{hyperref}
\usepackage{lipsum}
\begin{document}
\lipsum[1-5]
\hypertarget{eq1}{\begin{equation}
x=a
\end{equation}}
\lipsum[6-10]
\end{document}
\end{filecontents}
\begin{document}
%% https://tex.stackexchange.com/a/320594/4301
As shown in \href[pdfnewwindow]{foo.pdf#eq1}{(1)} we know that $x=a$.
You can find this equation on \href[pdfnewwindow]{foo.pdf#page.2}{page 2}.
%% Ulrike Fischer's comment at https://tex.stackexchange.com/a/320594/4301
You can also find this equation on \href[page=2]{foo.pdf}{page 2}.
\medskip\par
%% How do I get this to work?
\def\PageNum{2}%
\def\HrefPageOption{page=\PageNum}%
Alternatively, you can also find this equation on
%\href[\HrefPageOption]{foo.pdf}{page \PageNum}.
\end{document}

\expandafter\href\expandafter[\HrefPageOption– Ulrike Fischer Mar 07 '22 at 20:43\expandafterwithin the square brackets. – Peter Grill Mar 07 '22 at 21:36