这个问题与LaTeX 中描述列表项的参考名称,因为这和我正在做的一样。那里提供的答案在大多数情况下都有效,但我发现有一个案例无法正确标记宏。
问题描述
标签的正常行为是引用最后一个可标记的东西(\autoref{}
代替\ref{}
在某些情况下将验证这一点)。当在描述列表中用标签标记默认项目标签本身时,这并不是预期的效果(笔记:术语冲突……项目的标签\item [label] Text.
和标记项目标签\item [label\label{mylabel}] Text.
*)。因此,我们需要重新定义描述列表的项目。上面提到的问题就是这样的。
在描述列表中使用标签时,\phantomsection
需要为要标记的标签添加 :)。到目前为止对我来说很有意义。我想知道为什么下面的代码会失败(参见代码注释)。
目的
我描述了许多参数列表,而且通常一个函数的参数已经在其他地方描述过了。交叉引用非常方便。
代码
\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}
输出
有问题的代码(只需将上面的代码与相应的内容取消注释即可)
\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}
更新:此问题有一些后续问题可在此处找到:如何在描述列表键内使用超链接超链接?
答案1
\edef\@currentlabel{#1}
必须被替换,\protected@edef\@currentlabel{#1}
否则论证的扩展就会失败。
\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: http://tex.stackexchange.com/a/1248/13552
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
\let\orglabel\label
\let\label\@gobble
\phantomsection
\protected@edef\@currentlabel{#1}%
%\edef\@currentlabelname{#1}%
\let\label\orglabel
\orgdescriptionlabel{#1}%
}
\makeatother
\begin{document}
\section{Labels}
%\label{mymacro} %<-- This works (creates reference to Label)
\begin{description}
\item [\menu{Fruit}] The is a fruit. A plain item.
\item [\menu{Vegetables}\label{menukey::Vegetables}] 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 (comment out other mymacro label)
\end{description}
\section{References}
This is a menukeysmacro \ref{menukey::Vegetables} for fun.
Now we reference the mymacro \ref{mymacro}
\end{document}
编辑
真正的罪魁祸首是\texttt
大多数(或所有?)\text...
样式宏都(高度)脆弱。
另一种选择(不使用\protected@edef
)是使\texttt{}
命令更强大(etoolbox
为此需要包)
\robustify\texttt
但是如果 中出现其他脆弱命令,这个过程将变得繁琐\mymacro
。所以\protected@edef
也许是更好的方法。