I'd like to use \nameref
with hyperref
, but I want be able to render my document without hyperref
, too.
So I'd like to define a wrapper command around nameref
(not defining a "dummy" \nameref
myself), depending on whether \nameref
is defined.
The answer preferably works with any defined command, not just \nameref
.
From similar questions I tried to derive some solution that did not work:
\newcommand{\checkAndDef}[1]{%
\ifcsname#1\endcsname%
\newcommand{\refn}[1]{\nameref{#1}}%
\else%
\newcommand{\refn}[1]{[#1]}% no \nameref available
\fi%
}
\checkAndDef{nameref}%
Note the \checkAndDef
looks ugly, and I added it after I failed to do a literal check for \nameref
. I guess I would need to know more details about TeX and LaTeX to fix that than I do.
First solution modeled after werner's proposals (does not work):
\newcommand{\namerefcheck}{%
\ifcsname{}nameref\endcsname%
\newcommand{\refn}[1]{\nameref{##1}}%
\else%
\newcommand{\refn}[1]{[\detokenize{##1}]}% no \nameref available
\fi%
}
\AtBeginDocument{\namerefcheck}
This "solution" defines \refn
as if hyperref weren't loaded, regardless whether is is or not.
Note: If you replace {}nameref
with \nameref
, then it works (it seems to me).
\nameref
is available without hyperlinks.