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.

Mico
  • 506,678
  • 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 '20 at 05:16
  • @Mico does this mean I would not use \cref``/\Crefto create the reference? Ideally, for consistency purposes, I would like to stick with using the\cref/\Crefcommand to create references (whethercleverref` 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 '20 at 05: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 '20 at 05:32
  • @Mico I am also happy to not use the description environment if I can still achieve the same end: a named list item which I can then reference by name. – David Poxon May 10 '20 at 05:34
  • 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 '20 at 05:44
  • 1
    Okay then that sounds good to me :) – David Poxon May 10 '20 at 05:47

1 Answers1

2

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

Mico
  • 506,678
  • 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 '20 at 07:13
  • @DavidPoxon - Did you load the hyperref package? – Mico May 10 '20 at 07:24
  • I have the following \usepackage[hyperfootnotes=false]{hyperref} – David Poxon May 10 '20 at 07:25
  • @DavidPoxon - My divination skills are, sadly (but probably not surprisingly), utterly worthless. Showing me some tiny, unconnected text fragments is a fail-safe way of exposing this weakness. Sorry. I do know that my MWE compiles. If your program doesn't compile, it must be because it differs in one or more important aspects from mine. Which document class do you employ? What kind of description environment do you employ? – Mico May 10 '20 at 07:29
  • 2
    @DavidPoxon If you also use cleveref, you have to load hyperref and cleveref in correct sequence (cleveref last). – Sveinung May 10 '20 at 07: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 '20 at 08:04
  • You may also have a look at the (very advance) package enumitem-zref. – Sveinung May 10 '20 at 08:11
  • @Mico I am using enumitem version 3.9 and expdlist of unknown version. I am using the book document class. – David Poxon May 10 '20 at 08:13
  • @DavidPoxon - The expdlist package appears to have been updated most recently in 1999. (Hey, it was a fine year! I remember it well.) I would strongly recommend that you consider employing the (much newer and still actively maintained) enumitem package to modify the appearance of description-type list environments. If you need help transitioning from expdlist to enumitem, feel free to post a new query focused on that particular issue. – Mico May 10 '20 at 08:20
  • @Mico I am sorry - at a second glance, I misspoke. I only have enumitem installed. – David Poxon May 10 '20 at 08:22
  • @DavidPoxon - I guess you really, really don't like to part with information freely. So we've confirmed that your document loads the enumitem package. Now: What do you do with its machinery? Do you modify the description environment directly (and, if so, how), or do you create a bespoke description-like environment with special properties? As I mentioned earlier, my divination skills are worthless. Hence, do not assume that I will be able to guess out of nowhere what's going on in your document. – Mico May 10 '20 at 08:29
  • 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 '20 at 08:50