Suppose I defined an environment with an optional argument (using xparse
) as in
\documentclass{article}
\usepackage{hyperref,xparse}
\newcounter{exercisecounter}
\stepcounter{exercisecounter}
\DeclareDocumentEnvironment{exercise}{o}
{\IfNoValueTF{#1}{Exercise \theexercisecounter:}{Exercise \theexercisecounter (#1):}}
{}
\begin{document}
\begin{exercise}[My Exercise]
This is my exercise.
\end{exercise}
Above is \nameref{my exercise}, which is Exercise \ref{my exercise}.
\end{document}
If I'd like to reference the exercise's title and the exercise counter using nameref
and ref
, respectively, how would I go about doing it? In general, given an arbitrary environment with an optional argument, how would I got about telling hyperref
to use the optional argument as the title and a specified counter?
I understand that a solution would likely be based on using xpatch
, but this is the extent of my knowledge.
\nameref
is not expandable!