1

Is there a way to use cleveref to reference the name of an item in a description environment?

For example, if I have

\begin{description}
    \item[TEST\label{test}] test
\end{description}

I would like to reference it through \cref{test} and for the text to be TEST.

  • 1
    Does the solution have to be based on cleveref's machinery? If not, would a solution based on the \hyperlink/\hypertarget mechanism of the hyperref package be acceptable? – Mico May 10 at 5:16
  • @Mico does this mean I would not use \cref``/\Cref` to create the reference? Ideally, for consistency purposes, I would like to stick with using the \cref/\Cref command to create references (whether cleverref is used under the hood I have no strong opinions about. From an outcome perspective, if I had to use a different command but had consistent behaviour for all references throughout the document, I would not mind. – David Poxon May 10 at 5:20
  • 1
    cleveref works by greatly extending LaTeX's basic \label/\ref mechanism. LaTeX associates the argument of \label with the most recently incremented counter variable. (E.g., each time a \section directive is executive, the section counter is incremented.) Unlike the enumerate list environment, the description list environment does not increment a counter variable when an \item directive is encountered. You'd have to graft some kind of secondary counter mechanism onto the list environment -- and associate the new counters with the description list test items. Ugly... – Mico May 10 at 5:32
  • 1
    If you can use an enumerate environment, you (more precisely, cleveref) would be all set to go. However, the cross-reference call-out produced by \cref would be a number, not the implicit or explicit label associated with that number. This is why I was thinking about employing the \hyperlink/\hypertarget mechanism. – Mico May 10 at 5:44
  • 1
    Okay then that sounds good to me :) – David Poxon May 10 at 5:47
1

Building on the discussion led in the preceding comments, here's a solution that employs the \hypertarget/\hyperlink mechanism of the hyperref package to create cross-references that show text strings rather than numbers.

The commands \hypertarget and \hyperlink both take 2 arguments. \hypertarget creates a target; the first argument becomes the internal name of the target, and the second determines what's shown in the document. \hyperlink creates a link to the target created by \hypertarget. The first arguments of \hypertarget and \hyperlink must coincide, while the second arguments needn't, as is also shown in the following MWE (minimum working example).

\documentclass{article}
\usepackage[colorlinks,linkcolor=blue]{hyperref}

\begin{document}
\begin{description}
    \item[FOO] \dots
    \item[\hypertarget{list:test}{\textbf{TEST}}] \dots
    \item[BAR] \dots
\end{description}
As was demonstrated in the \hyperlink{list:test}{TEST} item of the preceding list, \dots
\end{document}

enter image description here

| improve this answer | |
  • Hmm, I getting these errors: Undefined control sequence. ...m[\hypertarget{lampstate}{\textbf{State}}] Use of \\smash doesn't match its definition. ...m[\hypertarget{lampstate}{\textbf{State}}] – David Poxon May 10 at 7:13
  • @DavidPoxon - Did you load the hyperref package? – Mico May 10 at 7:24
  • 2
    @DavidPoxon If you also use cleveref, you have to load hyperref and cleveref in correct sequence (cleveref last). – Sveinung May 10 at 7:59
  • 1
    I load cleveref after hyperref. I created a new document in Overleaf and used the same preamble in my document - this solution works. I'm not sure why it's not working in my document. – David Poxon May 10 at 8:04
  • 1
    @Mico I think this is the frustrating part - as far as I'm aware, I haven't modified the description environment in any way (in fact, this is the one and only place I use it in my document.) I have stopped a number of files from being imported, and I have finally got your solution to work! I will reincorporate each document and see which one causes things to fail. :) – David Poxon May 10 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.