0

I'd like to write, e.g., \xlabel[custom text]{dual}. That need not output anything, but whenever I write \ref{dual} (or, e.g., \xref{dual}), that should output "custom text".

There are two advantages.

  1. One can change the name of a concept later.
  2. The text "custom text" still serves as a link to the definition*, provided \usepackage{hyperref}.

A solution is given here: Defining custom labels. However, with hyperref it produces an error: "Paragraph ended before \Hy@setref@link was complete."

Does somebody have a solution to that? Even better, if the solution would make clicking the \ref jump to the location of the label instead of the previous numbered item (such as an equation or a theorem), but even such an approximate jumping would be better than none.

Another solution is given here Bold enumerate labels, non-bold reference to them. However, it requires one to add lots of code and a new enumbered item, whereas the original solution allows one to add numerous labels without changing the presentation at all.

Convexity
  • 342

1 Answers1

1

You can use \nameref which is part of hyperref:

\documentclass{article}

\usepackage{hyperref} \makeatletter \newcommand\xlabel[2][]{\phantomsection\def@currentlabelname{#1}\label{#2}} \makeatother \begin{document}

\nameref{blub}

\bigskip

blblb\xlabel[some text regarding blub]{blub}

\end{document}

enter image description here

More sophisticated systems are possible with packages like glossaries or acronym.

Ulrike Fischer
  • 327,261
  • Thanks, this might do. Do you know if journals have problems with hyperref; that is, should it be turned off before submitting to journals? If not, this might be a reasonable solution, although the hyperlink helps the reader (including the author while editing) only in the electronic versions (of the math journal or working file). – Convexity Jun 20 '20 at 17:33
  • 1
    it should work without hyperref too: simply load only nameref and remove the \phantomsection command. – Ulrike Fischer Jun 20 '20 at 17:36
  • Nice. This works like: "We call $t$ {\em positive}\xlabel[positive]{positive} if $t>0$, and {\em nonnegative}\xlabel[nonnegative]{nonnegative} if $t\ge0$. ... If $s$ is \nameref{positive}, then ..."

    Apparently, if such definitions are frequent, one can define \newcommand\xxlabel[1]{{\em #1}\xlabel[#1]{#1}} The main benefit in either is that, in the pdf file, such mentions "positive" appear as hyperlinks to the definition. In TexShop you also get a hover text but not in TeXStudio, it seems.

    – Convexity Jun 20 '20 at 20:17
  • These proved useful extensions: \newcommand{\emxlabel}[2]{{\em #1}\xlabel[#1]{#2}} %#1 is what you see, #2 is the link name Similarly, you can use the following for itemized lists: \newcommand{\itempxlabel}[1]{\item[\bf (#1)]\xlabel[(#1)]{#1}} – Convexity Jun 21 '20 at 09:07
  • To make the [first argument] equal to the {second} if not given, use this code: \usepackage{xifthen} \makeatletter \newcommand{\xlabel}[2][]{\phantomsection\def\@currentlabelname{\ifthenelse{\equal{#1}{}}{#2}{#1}}\label{#2}} \makeatother – Convexity Jun 21 '20 at 11:22
  • Add the following line to your .cwl file to allow for auto-completion of ref tags: \xref{label}#r Or just use \nameref{}, not \xref{}. – Convexity Jun 21 '20 at 11:52
  • Add the following lines to your .cwl file (and restart TeXstudio) to allow for the auto-completion of \xref labels (and to include \xlabel labels etc. into that list): \xref{label}#r \xlabel{label}#l \itempxlabel{label}#l \emxlabel{label}#l – Convexity Jun 21 '20 at 16:57