当我在环境中使用自定义标签时enumerate
,如下:
\documentclass{amsart}
\newtheorem{theorem}{Theorem}[section]
\usepackage{enumitem}
\begin{document}
\begin{theorem}Consider the following conditions
\begin{enumerate}
\item[(xy)] Condition 1 \label{a:cond:1}
\item[($\neg$)] Condition 2 \label{a:cond:2}
\item[($\sim$)] Condition 3 \label{a:cond:3}
\end{enumerate}
\end{theorem}
I want \verb|\eqref{a:cond:2}| to output ``($\neg$)'' instead of
\eqref{a:cond:2}
\end{document}
每个 \item 之后的 \label 命令都会将标签分配给定理,因此输出不是我想要的:
如果没有手动修改项目标签,则输出将符合预期:
\documentclass{amsart}
\newtheorem{theorem}{Theorem}[section]
\usepackage{enumitem}
\begin{document}
\begin{theorem}Consider the following conditions
\begin{enumerate}
\item Condition 1 \label{b:cond:1}
\item Condition 2 \label{b:cond:2}
\item Condition 3 \label{b:cond:3}
\end{enumerate}
\end{theorem}
\verb|\eqref{b:cond:2}| works correctly: \eqref{b:cond:2} \eqref{a:cond:2}
\end{document}
为什么会出现这种情况?我该如何防止它?
答案1
\item[(foo)]
\refstepcounter{enumi}
在环境中不使用enumerate
,因此\label
使用最新\@currentlabel
值,该值始终由设置\refstepcounter
,但在这种情况下,它将使用来自部分等计数器的值或任何之前使用过的计数器\refstepcounter
。
如果\item[foo]
要给出引用“foo”,则\@currentlabel
必须通过重新定义来添加\item[]
,这仅在环境本地完成enumerate
,这样,它不会更改\item
其他类似列表的环境的命令。
\documentclass{amsart}
\newtheorem{theorem}{Theorem}[section]
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{letltxmacro}
\makeatletter
\AtBeginDocument{%
\def\strip@@parentheses(#1){#1}
\LetLtxMacro\enumerate@@item\item
\AtBeginEnvironment{enumerate}{%
% Redefine \item only for enumerate
\RenewDocumentCommand{\item}{o}{%
\IfValueTF{#1}{% Check whether \item[...] is used
\enumerate@@item[#1]%
\protected@edef\@currentlabel{\strip@@parentheses#1}% Prepare the label
}{%
\enumerate@@item% Usage without []
}%
}%
}
}
\makeatother
\begin{document}
\begin{theorem}Consider the following conditions
\begin{enumerate}
\item[($xy$)] Condition 1 \label{a:cond:1}
\item[($\neg$)] Condition 2 \label{a:cond:2}
\item[($\sim$)] Condition 3 \label{a:cond:3}
\end{enumerate}
\end{theorem}
I want \verb|\eqref{a:cond:1}| to output ``($xy$)'' and it prints: \eqref{a:cond:1}
I want \verb|\eqref{a:cond:2}| to output ``($\neg$)'' and it prints: \eqref{a:cond:2}
I want \verb|\eqref{a:cond:3}| to output ``($\sim$)'' and it prints: \eqref{a:cond:3}
\end{document}