Hyperref-proof solution (1. works with hyperref, 2. also adds hyperlinks to the \xlabel):
You can use \nameref which is part of hyperref :
\documentclass{article}
\usepackage{hyperref}
\newcommand{\xref}{\nameref}
\makeatletter
\newcommand\xlabel[2][]{\phantomsection\def\@currentlabelname{#1}\label{#2}}
\makeatother
\begin{document}
We call $t$ {\em positive}\xlabel[positive]{pos} if $t>0$. Now ...
\bigskip
Assume that $s$ is \xref{pos}. Then ...
Recall the definition of \hyperref[pos]{positivity}
\end{document}
Now \xref{pos} prints "positive" with a hyperlink to the definition (xlabel). (The \hyperref above prints the customized text "positivity" instead of "positive". To produce hyperlinks without )
To avoid repetition, you can just write \emxlabel{positive}{pos} above once you define:
\newcommand{\emxlabel}[2]{{\em #1}\xlabel[#1]{#2}}
Similarly, you can use the following for itemized lists:
\newcommand{\itempxlabel}[1]{\item[\bf (#1)]\xlabel[(#1)]{#1}}
To make the [first argument] equal to the {second} if not given, use this code (or see here):
\usepackage{xifthen}
\makeatletter
\newcommand{\xlabel}[2][]{\phantomsection\def\@currentlabelname{\ifthenelse{\equal{#1}{}}{#2}{#1}}\label{#2}}
\makeatother
More sophisticated systems are possible with packages like glossaries or acronym. The above solution is essentially due to Ulrike Fischer.
Benefits:
- You can change the appearance of the \xref{pos} later on just by changing the first argument of \xlabel. (Also the other solutions have this benefit except that some of them stop working if you \usepackage{hyperref}.)
- \xref{pos} also produces a hyperlink. By clicking it you get to the definition (\xlabel). TexShop also shows a hover (mouseover) text (the definition). I don't know how to get the hovertext to TeXStudio.
Add the following lines to your .cwl file (and restart TeXstudio) to allow for auto-completion of \xref label (and to include \xlabel labels etc. into that list):
\xref{label}#r
\xlabel{label}#l
\itempxlabel{label}#l
\emxlabel{label}#l
If you use the {xifthen} formulation of \xlabel, then its .cwl line shoud read
\xlabel[nonlabel]{label}#l
\label
refers to the last\refstepcounter{<counter>}
. If you use your own counter like that than it isn't a problem. Numbering every line is a different challenge.\refstepcounter{...}
followed by a\label
. This would cause proper references I think. Also have a look on thezref
package. It provides a wide variety of referencing features.