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:
There are three defects with this output:
- The face of the reference is the same sans-serif as the label itself, but I want
normalfont
to match the surrounding text. - 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.) - 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}
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: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 ofenumitem
:This will result in: