21

Is there a way to reference just plain text with the \ref{} tag?

I have a table with two columns. Each column has content which links to a different part below. The reference in the first column is no problem, because it refers to an item. The second column however is just plain text, but I want to refer that to the other text as well. I want to do that, so that I only need to change the referenced text and the text in my table will automatically update (preferably a reference, which can't be clicked on).

Here is the rough outline.

Table

Col 1 | Col 2
\ref{sth:bla} & \ref{sth:text} \

The referenced part

\begin{description}
    \item[Something\label{sth:bla}] The Text \hfill \\
    Description!
\end{description}

So now I would want to reference "The Text" with \ref{sth:text}, so that I wouldn't have to change the text twice. As I said, preferably I would like to reference without actually making if clickable.

The answer given by Werner does work fine to some point. Here is an example, where it doesn't seem to work correctly anymore. How would I use regular referencing in the document now?

\documentclass{article}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\makeatletter
    \newcommand*{\textlabel}[2]{%
        \edef\@currentlabel{#1}% Set target label
        \phantomsection% Correct hyper reference link
        #1\label{#2}% Print and store label
    }
\makeatother
\begin{document}
\section{A section}

This is a table:

\begin{center}
\begin{tabular}{l|l}
 Col 1 & Col 2 \\
\ref{sth:bla} & \ref{sth:text} \\
\ref{Blub} & Text \\
\end{tabular}
\end{center}

The items:

\begin{description}
   \item[\textlabel{Something}{sth:bla}] \textlabel{The Text}{sth:text} \hfill \\
   Description! \\
   \item[Blub\label{Blub}] Something \hfill \\
   Another description!
\end{description}
\end{document}

The normal reference tag will now be replaced by the text content of the textlabel.

3
  • 1
    Please don't cross-post (see Reference an item without displaying the text). Rather flag it for migration between sites, or delete and repost. Also, use the same OpenID when registering on the StackExchange network, otherwise your accounts won't be associated with one another.
    – Werner
    May 7, 2012 at 23:39
  • You have to make a compromise here. When you use \label{<lab>}, LaTeX only stores a counter (which is a number) and a page (which is also a number). hyperref stores a little extra, but it boils down to the same. If you want to store text in an ad-hoc fashion, then you have to use a custom package/macro (say \textlabel) in order to change the default behaviour. Of course \label-\ref alone will not work while \textlabel-\ref will, since it provides that alternative interface (away from the default). In short, use \textlabel, not \label...
    – Werner
    May 8, 2012 at 3:05
  • ...an alternative may be to use ordinary macros like \newcommand{\blah}{Blah} and then later use \blah. However, this requires you to use an auxiliary file so that the macro is available both before and after it is defined. Using \label-\ref circumvents this requirement by storing information in the .aux file to be used throughout the document (both before & after) it is made.
    – Werner
    May 8, 2012 at 3:06

1 Answer 1

21

Here's one way of achieving it:

enter image description here

\documentclass{article}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\makeatletter
\newcommand*{\textlabel}[2]{%
  \edef\@currentlabel{#1}% Set target label
  \phantomsection% Correct hyper reference link
  #1\label{#2}% Print and store label
}
\makeatother
\begin{document}
\section{A section}

This is a table:

\begin{center}
\begin{tabular}{l|l}
  Col 1 & Col 2 \\
  \ref{sth:bla} & \ref{sth:text}
\end{tabular}
\end{center}

And here are some items:

\begin{description}
    \item[\textlabel{Something}{sth:bla}] \textlabel{The Text}{sth:text} \hfill \\
    Description!
\end{description}
\end{document}

You would use \textlabel{<text>}{<label>} in order to mark and print the label that you can retrieve later as \ref{<label>}. Note that \textlabel both prints <text> and marks the label (using \label{<label>} internally).

6
  • Hmm. It works fine, when I copy and paste into a new document. However, when I try to just use the /makeatletter etc. it doesn't work. Can another /makeatletter definition be at fault?
    – cherrung
    May 7, 2012 at 23:46
  • 1
    @cherrung: You have to surround the \textlabel definition with \makeatletter and \makeatother. Best if you keep it that way. Other \makeatletters and \makeatothers can be elsewhere. If this still doesn't solve your problem, edit your original question with the updated details that is causing the problem (preferably in the form of a minimal working example / MWE).
    – Werner
    May 7, 2012 at 23:49
  • This solution works I guess, but then I would need to use \textlabel everywhere. Oh well... I will just stick to changing the text by hand then. Isn't there a way to get a table of a description?
    – cherrung
    May 8, 2012 at 16:25
  • How would you do if you wanted to add the functionality to nameref in stead of ref?
    – 7heViking
    Oct 19, 2015 at 19:54
  • @FireFly3000: Use \textlabel and then \nameref?
    – Werner
    Oct 19, 2015 at 20:15

You must log in to answer this question.

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