2

I wonder whether I can change displayed text in \ref globally for a \label referencing a numbered environment. Concretely:

Let's suppose I have a theorem called "Important Proof".

\begin{theorem}[Important Proof]\label{IP}
Trivial.
\end{theorem}

Then \ref{IP} would display something like 1.1.1. I however want to change that text only sporadically, so that this specific proof gets displayed e.g. with Important Proof, but all other proofs stay unaffected.

What did I mean with globally? I saw some workarounds using \hyperref every single time, but this does not seem really convincing; I would like to have some kind of redefinition, so that I can just use something like \ref s.t. I don't have to insert the text every single time manually.

  • 1
    The nameref package provides \nameref that works like that. Redefining \ref globally to be \nameref won't be a good idea though. – Werner Dec 24 '19 at 16:30
3
\documentclass{article}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}
\begin{document}
\begin{theorem}[Important Proof]%
\makeatletter\def\@currentlabel{Important Proof}\makeatother
\label{IP}
Trivial.
\end{theorem}

\ref{IP}
\end{document}

enter image description here

| improve this answer | |
  • Great, thanks! How did you infer that one must redefine \@currentlabel? Just by reading the hyperref documentation? – J.N. Dec 26 '19 at 16:18
  • I know a bit about the LaTeX and hyperref code. – Ulrike Fischer Dec 26 '19 at 16:48
  • Sure, I just wonder how to get to the solution as a mere mortal :D – J.N. Dec 26 '19 at 17:47
  • I'm a mere mortal and I learned it because it is not a secret. You can find over 600 hits with \@currentlabel and more if you google. At some time in the past I probably read one answer about it. – Ulrike Fischer Dec 26 '19 at 20:20
0

I don't think this would be possible simply without redefining \ref, which is probably considerably more work than you're interested in (or than the problem is really worth). However, you could define a wrapper macro that would take care of what you want. Make sure you use \usepackage{ifthen} (this is a standard LaTeX package), and define a macro \nref like so:

\def\nref#1{%
    \ifthenelse{\equal{#1}{IP}}{%
        ``Important Proof''%
    }{%
        \ref{#1}%
    }%
}%

Then, anytime you'd use \ref, use \nref instead. This resulted in the following references, when I defined two figures:

Results

I hope that answers your difficulty.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.