# Defining custom labels with hyperref

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".

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.

• is the label always before the reference? – Ulrike Fischer Jun 20 at 17:18
• Not necessarily. Admittedly, the "\ref{dual}" could be "\xref{dual}" or the like (now I edited that into the question). – Convexity Jun 20 at 17:21

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}



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

• 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 at 17:33
• it should work without hyperref too: simply load only nameref and remove the \phantomsection command. – Ulrike Fischer Jun 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 at 20:17
• 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 at 11:22