Labeling a text and referencing it later
\label
will refer to the current value of \@currentlabel
; just use this feature.
\documentclass{article}
\MakeRobust{\ref}% avoid expanding it when in a textual label
\makeatletter
\newcommand{\labeltext}[2]{%
\@bsphack
\csname phantomsection\endcsname % in case hyperref is used
\def\@currentlabel{#1}{\label{#2}}%
\@esphack
}
\makeatother
\begin{document}
\section{Test}\label{sec-test}
Some text and a textual label\labeltext{Some text in section~\ref{sec-test}}{try}
\section{Again}
Here it is: ``\ref{try}''
\end{document}
If your LaTeX kernel is before 2015/01/01, update it. Until you do, add \usepackage{fixltx2e}
in order to have \MakeRobust
available. Making \ref
robust is not needed if you load hyperref
.
Something like this? This writes the text explicitly to the .aux
file and provides a hyperlink to it. Use \nameref
to get the label content, not the label number (which is given by \ref
)
With more information a better solution could be given.
\documentclass{article}
\usepackage{blindtext}
\usepackage{hyperref}
\usepackage{nameref}
\newcounter{mylabelcounter}
\makeatletter
\newcommand{\labelText}[2]{%
#1\refstepcounter{mylabelcounter}%
\immediate\write\@auxout{%
\string\newlabel{#2}{{1}{\thepage}{{\unexpanded{#1}}}{mylabelcounter.\number\value{mylabelcounter}}{}}%
}%
}
\makeatother
\begin{document}
\blindtext[5]
\begin{center}
In \nameref{label:text} we have
\end{center}
\section{First} \label{firstsection}
\blindtext[5]
\labelText{This \textsc{is} a text that is also a tag}{label:text}
\end{document}