[Tex/LaTex] Label & reference description items with enumitem

cross-referencingenumitemhyperref

Aim

I want to be able to label and then reference items in description environments created using the enumitem package.

What does not work

The following example modifies the accepted answer at How can I label / reference description items that contain macros by name?.

\documentclass[12pt]{article}

\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\usepackage{enumitem}

\newlist{describe}{description}{1}
\setlist[describe,1]{%
  font=\normalfont\textsf,
  itemindent=0pt,
  wide,
  itemsep=0pt,topsep=2pt,
}

% Redefine Description List Items 
%    (source: http://tex.stackexchange.com/a/1248/13552)
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \protected@edef\@currentlabel{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}

\Large
\section{Good eats}

\begin{describe}

\item [Fruit:] Oranges and apples.
\item [Vegetable:\label{item:veggie}] Kale and potatoes. 

\end{describe}

\newpage 
Refer to \ref{item:veggie} above on \cpageref{item:veggie}.

\end{document}

Output:

Faulty item reference

There are three defects with this output:

  1. The face of the reference is the same sans-serif as the label itself, but I want normalfont to match the surrounding text.
  2. There is superfluous punctuation (:) in the reference, but I want just the name of the item and not any trailing punctuation. (Note that for different describe environments, the trailing punctuation could be different, e.g., a period instead of a colon.)
  3. There is extra blank space between the reference and the text "above on …" that follows it.

How can these defects be repaired?

What does work but uses awkward syntax

The following version of the source, based upon the accepted answer at Reference name of description list item in LaTeX, cures all three defects — but at the cost of an awkward syntax within the \item optional argument.

What I find syntactically awkward is that the optional argument to \item includes a new macro \namedlabel that takes two arguments — the label identifier and the item name — and then any punctuation has to go outside and after that 2nd argument.

\documentclass[12pt]{article}

\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\usepackage{enumitem}

% From 
% https://tex.stackexchange.com/questions/1230/reference-name-of-description-list-item-in-latex
\makeatletter
\def\namedlabel#1#2{\begingroup
    #2%
    \def\@currentlabel{#2}%
    \phantomsection\label{#1}\endgroup
}
\makeatother

\newlist{describe}{description}{1}
\setlist[describe,1]{%
  font=\normalfont\textsf,
  itemindent=0pt,
  wide,
  itemsep=0pt,topsep=2pt,
}

\crefname{page}{page}{page}

\begin{document}
\Large
\section{Good eats}

\begin{describe}

\item[Fruit:] Oranges and apples.

% Awkward syntax in optional argument to \item below:
\item[\namedlabel{item:veggie}{Vegetable}:] Kale and potatoes.

\end{describe}

%\newpage

Refer to \ref{item:veggie} above on \cpageref{item:veggie}.

\end{document}

Output:
OK output but awkward syntax

Best Answer

I (like gernot in his comments to your question) see no problem with the syntax of \namedlabel. Nevertheless you can indeed define additional commands to mark parts of the item label to become not part of the reference. You can also define how automatic parts of the argument of \descriptionlabel should be interpreted while the expanding it to define the reference. And you could add a command to define which part of the argument should got to the reference. Here is an example that does automatically remove the \hfil from the reference (but not the font switch nor the :), manually removes the : or as an alternative defines the whole reference part:

\documentclass[12pt]{article}

\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\usepackage{enumitem}

\newlist{describe}{description}{1}
\setlist[describe,1]{%
  font=\normalfont\textsf,
  itemindent=0pt,
  wide,
  itemsep=0pt,topsep=2pt,
}

% Redefine Description List Items 
%    (source: http://tex.stackexchange.com/a/1248/13552)
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\newcommand*{\@restrictlabeltext}[1]{#1\protected@edef\@currentlabel{#1}}
\newcommand*{\nolabel}[1]{#1}%
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \let\orig@hfil\hfil
  \def\hfil{}%
  \let\nolabel\@gobble
  \let\restrictlabeltext\@firstofone
  \phantomsection
  \protected@edef\@currentlabel{#1}%
  \let\hfil\orig@hfil
  \let\label\orglabel
  \let\restrictlabeltext\@restrictlabeltext
  \orgdescriptionlabel{#1}%
}
\makeatother

\begin{document}

\Large
\section{Good eats}

\begin{describe}

\item [Fruit\nolabel{:}\label{item:fruit}] Oranges and apples.
\item [\restrictlabeltext{Vegetable}:\label{item:veggie}] Kale and potatoes. 

\end{describe}

\newpage 
Refer to \ref{item:veggie} above on \cpageref{item:veggie} or \ref{item:fruit}
on \cpageref{item:fruit}.

\end{document}

You could also make the : active and define it to be empty or \relax while the \protected@edef. You could even temporary redefine the font commands. But I don't think, that any of this suggestions are really better than your solution.


As an completely different alternative you could put the colon as part of the label format and patch \phantomsection and setting of \@currentlabel into the description list of enumitem:

\documentclass[12pt]{article}

\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\usepackage{enumitem}
\usepackage{xpatch}

\newlist{describe}{description}{1}
\setlist[describe,1]{%
  font=\normalfont\textsf,
  itemindent=0pt,
  wide,
  itemsep=0pt,topsep=2pt,
  format={\normalfont\textsfcolor}
}
\newcommand*{\textsfcolor}[1]{\textsf{#1:}}
\makeatletter
\xpatchcmd{\enit@description@i}{%
  \labelsep\z@
}{%
  \phantomsection
  \let\org@label\label
  \let\label\@gobble
  \protected@edef\@currentlabel{##1}%
  \let\label\org@label
  \labelsep\z@
}{}{\undefined}
\makeatother

\begin{document}

\Large
\section{Good eats}

\begin{describe}

\item [Fruit\label{item:fruit}] Oranges and apples.
\item [Vegetable\label{item:veggie}] Kale and potatoes. 

\end{describe}

\newpage 
Refer to \ref{item:veggie} above on \cpageref{item:veggie} or \ref{item:fruit}
on \cpageref{item:fruit}.

\end{document}

This will result in:

enter image description here

Related Question