16

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.

4
  • With \ref{label:text} the text "This is a text that is also a tag" should appear, I assume?
    – user31729
    Commented Oct 4, 2015 at 14:04
  • 2
    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
    – touhami
    Commented 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
    Commented Oct 4, 2015 at 15:35
  • @ChristianHupfer yes you're right.
    – touhami
    Commented Oct 4, 2015 at 15:44

3 Answers 3

8

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

3
  • 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? Commented Feb 14, 2016 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
    – user31729
    Commented Feb 14, 2016 at 16:37
  • 1
    @FiboKowalsky: See the update please!
    – user31729
    Commented Feb 14, 2016 at 16:38
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

3
  • With TeX Live 2023, the second run gives an error ! Missing \endcsname inserted.
    – Stephen
    Commented Aug 4, 2023 at 13:57
  • @Stephen Fixed with recommendations for older LaTeX versions
    – egreg
    Commented Aug 4, 2023 at 14:19
  • Thank you!@egreg
    – Stephen
    Commented Aug 4, 2023 at 15:06
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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .