Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

(Repost from Stack Overflow)

I'd like to refer to a description list item by name instead of number. To that effect, I've added labels to each item, but when referencing them I only get the name of the section, not of the list item. How can I change it to show a custom label for each item?

\section{Definitions}
\begin{description}
    \item [Vehicle\label{itm:vehicle}] Something
    \item [Bus\label{itm:bus}] A type of \nameref{itm:vehicle}
    \item [Car\label{itm:car}] A type of \nameref{itm:vehicle} smaller than a \nameref{itm:bus}
\end{description}

The result is something like this:

1 Definitions

Vehicle Something
Bus A type of Definitions
Car A type of Definitions smaller than a Definitions

I'd like to have the following:

1 Definitions

Vehicle Something
Bus A type of Vehicle
Car A type of Vehicle smaller than a Bus

An alternative solution would be to use subsections and display them as a definition list. Anyone know how to do it?

The best answer on Stack Overflow referenced a \makeatletter hack:

\makeatletter
\def\namedlabel#1#2{\begingroup
   \def\@currentlabel{#2}%
   \label{#1}\endgroup
}
\makeatother
...
\section{Definitions}
\begin{description}
    \item [Vehicle\namedlabel{itm:vehicle}{Vehicle}] Something
    \item [Bus\namedlabel{itm:bus}{Bus}] A type of \ref{itm:vehicle}
    \item [Car\namedlabel{itm:car}{Car}] A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{description}

It works, with the caveat that the links lead back to the section header, not to the list item. It would be nice to use something native which doesn't break \ref.

share|improve this question

3 Answers 3

up vote 10 down vote accepted

To expand a bit on some of the other answers: here is a modification that does not change the syntax of the description environment:

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}

\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}

\section{Definitions}
\begin{description}
    \item [Vehicle\label{itm:vehicle}] Something
    \item [Bus\label{itm:bus}] A type of \ref{itm:vehicle}
    \item [Car\label{itm:car}] A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{description} 

The item `\ref{itm:bus}' is listed on page~\pageref{itm:bus} in section~\nameref{itm:bus}.

\end{document}
share|improve this answer
    
Excellent stuff; now \ref even works like expected outside of the description environment. –  l0b0 Aug 10 '10 at 12:44
    
@Martin Heller @Andrew Stacey Do you know if it is possible to have the references in spacedlowsmallcaps font? I am using classicthesis, where description items are written using this font (the code from the style file: \renewcommand{\descriptionlabel}[1]{\hspace*{\labelsep}\spacedlowsmallcaps{#1}}‌​‌​), so when I am referencing the items I would like them to look the same as in the list. –  Elena Nov 20 '13 at 15:45

Here's a version that seems to work. There are, I think, two separate issues with what you are trying to do. One is simply to get the labels to be what you want and not according to some automatic numbering scheme. That's what the SO hack does. The other issue is to ensure that these labels refer to what you think that they are referring to. The SO hack does not address this. The point is that a label is both a label and a marker. In normal TeX, this dual role isn't visible because the marker isn't really used (well, it's used to figure out what the label should contain, but you want to override that). But when using a hyperlink package, such as hyperref.sty, the marker means something again: it's where the hyperlink goes to.

So you need to both change the label and have the marker at the right point. The former is solved by the SO hack, but the latter (as I said) is not. Either you can add the markers in explicitly, or you can subvert something that would put them in automatically. The reason they aren't there is that you are using the description environment which doesn't automatically put in the markers. By using a different listing environment, say enumerate, which does get the markers put in, we can get the desired behaviour. It's still a "hack", I'm afraid, but not a very big one.

The following does it, as far as my testing shows:

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\newcommand{\labitem}[2]{%
\def\@itemlabel{\textbf{#1}}
\item
\def\@currentlabel{#1}\label{#2}}
\makeatother

\begin{document}
\begin{enumerate}
\labitem{Vehicle}{itm:vehicle} Something
\labitem{Bus}{itm:bus} A type of \ref{itm:vehicle}
\labitem{Car}{itm:car} A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{enumerate}

Let's refer to \ref{itm:vehicle} \ref{itm:bus} and \ref{itm:car}

\end{document}
share|improve this answer
    
+1, interesting alternative approach –  l0b0 Aug 10 '10 at 12:40

Following up on the second suggesiton, assuming you're using hyperref, you could change the definition to:

\makeatletter
\def\namedlabel#1#2{\begingroup
   \def\@currentlabel{#2}%
   \phantomsection\label{#1}\endgroup
}
\makeatother

The phantomsection should anchor backreference links to the item.

There might be a way to do this using the enumitem package too, but I'd have to look further into it.

share|improve this answer
1  
+1, great to get correct links! –  l0b0 Aug 10 '10 at 12:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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