# Labeling a text and referencing it later

Is there a way to attach a label to a string and then refer to that string with a reference?

For example,

\labelText{This is a text that is also a tag}{label:text}


and then refer to this string with \ref{label:text} where the string will serve as a tag itself.

Why I need it? I am writing a pseudo code and using a command go to #line number but the line number might change if I insert extra line in the code. With referencing this will be avoided.

• With \ref{label:text} the text "This is a text that is also a tag" should appear, I assume?
– user31729
Oct 4, 2015 at 14:04
• do you know about \hypertarget{label}{target caption} and \hyperlink{label}{link caption} from hyperref package texdoc.net/texmf-dist/doc/latex/hyperref/manual.html#x1-140004 Oct 4, 2015 at 15:15
• @touhami: That's another possibility but does not replicate the text (and that's what's requested -- at least I've understood the question this way)
– user31729
Oct 4, 2015 at 15:35
• @ChristianHupfer yes you're right. Oct 4, 2015 at 15:44

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)

\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}


• It gives me error when adding some formatting in the tag: \labelText{This {\sc is} a text that is also a tag}{label:text} Is it possible to accommodate this option in tags? Feb 14, 2016 at 16:32
• @FiboKowalsky: First of all: Don't use the outdated \sc command. Second: Of course the approach with writing will fail until \unexpanded or \protected@write is used. I'll change
– user31729
Feb 14, 2016 at 16:37
• @FiboKowalsky: See the update please!
– user31729
Feb 14, 2016 at 16:38

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

Adding a second, completely different answer that applies my crossreftools package (Needs version 0.6, from DropBox Link)

\documentclass{article}

\usepackage{blindtext}
\usepackage{hyperref}

\usepackage{crossreftools}

\makeatletter

\def\labelText{%
\@ifstar{\@labeltextstarred}{\@labeltext}
}
\newcommand{\@labeltextstarred}[2]{%
\crtcrossreflabel*{#1}[#2]%
}

\newcommand{\@labeltext}[2]{%
\crtcrossreflabel{#1}[#2]%
}

\makeatother

\begin{document}

\labelText{Sample with holes}{label:text} \labelText*{and now for something completely different}{other:text}

In \crtlnameref{label:text} or \crtunameref{other:text} or \crtnameref{label:text}

\end{document}