This question is related to Reference name of description list item in LaTeX, because that is the same as what I am doing. The answer provided there works in most cases, but I found a case where it fails to label a macro properly.
Description of Problem
The normal behavior of labels is to reference last labelable thing (\autoref{}
in place of \ref{}
will verify this in some cases). This is not the desired effect when labeling the default item labels themselves in description lists with labels (note: terminology conflict…labels of items \item [label] Text.
and labeling the item labels \item [label\label{mylabel}] Text.
*). Therefore, we need to redefine items for description lists. The question mentioned above does this.
When using labels within description lists, a \phantomsection
needs to be added for the labels to label :). Makes sense so far to me. I'd like to know why the code fails below (see notes in code).
Purpose
I am describing many lists of parameters and oftentimes, the parameters for one function have already been described elsewhere. Cross-references are then extremely convenient.
Code
\documentclass{article}
\usepackage{fontspec} % xelatex ext font support
\usepackage{enumitem} % Add easy custom list support
\usepackage{menukeys} % Demonstrate labeling of macros
\usepackage{hyperref} % Add ref support
\setlength\parindent{0pt} % Globally set indentation for new paragraphs
\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=30pt,itemindent=\dimexpr-20pt-\labelsep\relax} % Global Setup Description List
\newcommand{\mymacro}[1]{\texttt{#1}} % my custom macro
\makeatletter % Redefinition of Description List Items source: https://tex.stackexchange.com/a/1248/13552
\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{Labels}
\label{mymacro} %<-- This works (comment this out when testing my problem)
\begin{description}
\item [Fruit] The is a fruit. A plain item.
\item [\menu{Vegetables}\label{menukeysmacro}] This is a vegetable. Macro from the \texttt{menukeys} package.
%\item [\mymacro{Meat}\label{mymacro}] This is some meat. Custom macro. % <-- This does not work (This should label \mymacro{Meat})
\end{description}
\section{References}
This is a menukeysmacro \ref{menukeysmacro} for fun.
Now we reference the mymacro \ref{mymacro}.
\end{document}
Output
Problematic Code (just code above with the appropriate things uncommented)
\documentclass{article}
\usepackage{fontspec} % xelatex ext font support
\usepackage{enumitem} % Add easy custom list support
\usepackage{menukeys} % Demonstrate labeling of macros
\usepackage{hyperref} % Add ref support
\setlength\parindent{0pt} % Globally set indentation for new paragraphs
\setlist[description]{style=nextline,labelwidth=0pt,leftmargin=30pt,itemindent=\dimexpr-20pt-\labelsep\relax} % Global Setup Description List
\newcommand{\mymacro}[1]{\texttt{#1}} % my custom macro
\makeatletter % Redefinition of Description List Items source: https://tex.stackexchange.com/a/1248/13552
\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{Labels}
\begin{description}
\item [Fruit] The is a fruit. A plain item.
\item [\menu{Vegetables}\label{menukeysmacro}] This is a vegetable. Macro from the \texttt{menukeys} package.
\item [\mymacro{Meat}\label{mymacro}] This is some meat. Custom macro. % <-- This does not work (This should label \mymacro{Meat})
\end{description}
\section{References}
This is a menukeysmacro \ref{menukeysmacro} for fun.
Now we reference the mymacro \ref{mymacro}.
\end{document}
Update: This question has some follow-up question to be found here: How can I use hyperref hyperlinks inside of description list keys?
mymacro
label refer to Fruit???\label
, it refers to the last thing that was said to be "labelable".\label
command uses the last counter being used inrefstepcounter