15

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.

lockstep
  • 250,273
  • With \ref{label:text} the text "This is a text that is also a tag" should appear, I assume? –  Oct 04 '15 at 14:04
  • 2
    do you know about \hypertarget{label}{target caption} and \hyperlink{label}{link caption} from hyperref package http://texdoc.net/texmf-dist/doc/latex/hyperref/manual.html#x1-140004 – touhami Oct 04 '15 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) –  Oct 04 '15 at 15:35
  • @ChristianHupfer yes you're right. – touhami Oct 04 '15 at 15:44

3 Answers3

12

\label will refer to the current value of \@currentlabel; just use this feature.

\documentclass{article}

% for LaTeX before 2015 uncomment the two following lines %\usepackage{fixltx2e} %\MakeRobust{\ref}

% for LaTeX between 2015 and 2021 uncomment the following line %\MakeRobust{\ref}

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

enter image description here

egreg
  • 1,121,712
7

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}

enter image description here

  • 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? – Fibo Kowalsky Feb 14 '16 at 16:32
  • 1
    @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 –  Feb 14 '16 at 16:37
  • 1
    @FiboKowalsky: See the update please! –  Feb 14 '16 at 16:38
2

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}