使用 enumitem+cleveref 的列表中 \ref 和 \cref 的不同输出

使用 enumitem+cleveref 的列表中 \ref 和 \cref 的不同输出

我正在使用enumitemcleveref包,并且我想强制列表的\ref\cref采取不同的行为{enumerate}

更准确地说,每当我在定理环境中有列表时,我都会向 so 添加一个label=...ref=...选项enumerate,以包含定理前缀(Thm、Lem、Prop、...)和相应的数字。然而,在定理的证明中,我想避免这种多余的前缀,我希望通过指定\ref和命令的不同行为来实现这一点。如果为环境提供一个选项,\cref问题就会得到解决,但事实并非如此。enumitemcref=...enumerate

编辑:我改变了 MWE 以消除可能出现的歧义。

我发布了以下 MWE,希望它可以帮助澄清我想要的内容:具体来说,(请注意\ref\cref在指代被证明的同一定理中的项目或外部的定理,但排版相同)我想在定理证明中引用定理的项目时消除前缀“Thm. 1.2”。

是的,一切都应该兼容hyperref...

\documentclass[english]{amsart}
\usepackage[english]{babel}
\usepackage{enumitem}
\usepackage[colorlinks=true]{hyperref}
\usepackage{amsthm}
\usepackage{cleveref}

\newtheorem{thm}{Theorem}[section]
% cleveref options for thm
    \crefname{thm}{Thm.}{Thm.}
    \crefformat{thm}{#2Thm. #1#3}
% cleveref options for enumi: remove "item "
\crefformat{enumi}{#2#1#3}

\begin{document}
\section{First section}
\begin{thm}\label{Thm:One}
The following properties hold:
\begin{enumerate}[{label=(\alph*)},{ref=\cref{Thm:One}(\alph*)}]
    \item\label{Thm:One:1} \(1>0\)
    \item\label{Thm:One:2} \(0<1\)
\end{enumerate}
\end{thm}

\begin{thm}\label{Thm:Two}
The following properties hold:
\begin{enumerate}[{label=(\alph*)},{ref=\cref{Thm:Two}(\alph*)}]
    \item\label{Thm:Two:1} \(2>1\)
    \item\label{Thm:Two:2} \(1<2\)
\end{enumerate}
\begin{proof}
\ref{Thm:Two:1} follows from \cref{Thm:One:1} by adding 1 on both sides and similarly \ref{Thm:Two:2} follows from \cref{Thm:One:2}.
\end{proof}
\end{thm}
\end{document}

输出如下 enter image description here

答案1

这是一个可能的解决方案(如果我正确理解了这个问题)。

关键是在环境开始时使用不同的\crefformat内部,并在结束时将其恢复为旧的内部。proofproof

这是通过使用\AtBeginEnvironment{proof}{...}和实现的\AtEndEnvironment{proof}{...}

\documentclass[english]{amsart}
\usepackage[english]{babel}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage[colorlinks=true]{hyperref}
\usepackage{amsthm}
\usepackage{cleveref}

\newtheorem{thm}{Theorem}[section]
% cleveref options for thm
    \crefname{thm}{Thm.}{Thm.}
    \crefformat{thm}{#2Thm. #1#3}
\newtheorem{lem}[thm]{Lemma}
% cleveref options for lem
\crefname{lem}{Lem.}{Lem.}
\crefformat{lem}{#2Lem. #1#3}
% cleveref options for enumi: remove "item "
\crefformat{enumi}{#2#1#3}

\AtBeginEnvironment{proof}{%
  \crefformat{lem}{#2 #1#3}%
}

\AtEndEnvironment{proof}{%
  \crefformat{lem}{#2Lem. #1#3}%
}

\begin{document}
\section{First section}
\begin{thm}\label{Thm:One}
The following properties hold:
\begin{enumerate}[{label=(\alph*)},{ref=\cref{Thm:One}(\alph*)}]
    \item\label{Thm:One:1} \(1>0\)
    \item\label{Thm:One:2} \(0<1\)
\end{enumerate}
\end{thm}

\begin{lem}\label{Lem:Two}
  The following properties hold:
  \begin{enumerate}[{label=(\alph*)},{ref=\cref{Lem:Two}(\alph*)}]
  \item\label{Lem:Two:1} \(2>1\)
  \item\label{Lem:Two:2} \(1<2\)
  \end{enumerate}
\begin{proof}
  \ref{Lem:Two:1} follows from \cref{Thm:One:1} by adding 1 on both sides and similarly \ref{Lem:Two:2} follows from \cref{Thm:One:2}.
\end{proof}
\end{lem}

Original reference is: \ref{Lem:Two:1}
\end{document}

更新

\documentclass[english]{amsart}
\usepackage[english]{babel}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage[colorlinks=true]{hyperref}
\usepackage{amsthm}
\usepackage{cleveref}



\newtheorem{thm}{Theorem}[section]
% cleveref options for thm
\crefname{thm}{Thm.}{Thm.}
\crefformat{thm}{#2Thm. #1#3}
\newtheorem{lem}[thm]{Lemma}
% cleveref options for lem
\crefname{lem}{Lem.}{Lem.}
\crefformat{lem}{#2Lem. #1#3}
% cleveref options for enumi: remove "item "
\crefformat{enumi}{#2#1#3}



\newcommand{\Lemcrefext}[1]{%
 \begingroup
 \crefformat{lem}{##2Lem. ##1##3}
 \cref{#1}%
 \endgroup
}

\newcommand{\Thmcrefext}[1]{%
 \begingroup
 \crefformat{lem}{##2Lem. ##1##3}
 \cref{#1}%
 \endgroup
}


\AtBeginEnvironment{proof}{%
  \crefformat{lem}{#2 #1#3}
}


\AtEndEnvironment{proof}{%
  \crefformat{lem}{#2Lem. #1#3}
}

\begin{document}
\section{First section}
\begin{thm}\label{Thm:One}
The following properties hold:
\begin{enumerate}[{label=(\alph*)},{ref=\cref{Thm:One}(\alph*)}]
    \item\label{Thm:One:1} \(1>0\)
    \item\label{Thm:One:2} \(0<1\)
\end{enumerate}
\end{thm}

\begin{lem}\label{Lem:Two}
  The following properties hold:
  \begin{enumerate}[{label=(\alph*)},{ref=\cref{Lem:Two}(\alph*)}]
  \item\label{Lem:Two:1} \(2>1\)
  \item\label{Lem:Two:2} \(1<2\)
  \end{enumerate}
\begin{proof}
  \ref{Lem:Two:1}  follows from \cref{Thm:One:1} by adding 1 on both sides and similarly \ref{Lem:Two:2} follows from \cref{Thm:One:2}. However, in \Lemcrefext{Lem:Three} and \Thmcrefext{Thm:One} it will be shown that...
\end{proof}
\end{lem}

\begin{lem}\label{Lem:Three}
Foo
\end{lem}

Original reference is: \ref{Lem:Two:1}
\end{document}

enter image description here

答案2

这是一个解决方案,它使用 (a)cleveref\crefalias设备和 (b) 两个专用枚举环境,分别名为enumthm和,分别在名为和 的enumcor环境中使用。这个想法是,这允许您独立使用和- 它们的输出仅在标签(“Thm。”,“Cor。”)是否将前缀为与交叉引用的定理或推论相关的复合“数字”时有所不同。thmcor\ref\cref

顺便说一句,在类似定理的环境中,对于枚举,我会使用直立的而不是斜体的字母“数字”——(a)、(b)等。

enter image description here

\documentclass[english]{amsart}
\usepackage{babel}
\usepackage{enumitem}  % for '\newlist' and '\setlist' macros
\usepackage[colorlinks=true]{hyperref}
%%\usepackage{amsthm}  % is loaded automatically by amsart document class
\usepackage[nameinlink]{cleveref}

\newtheorem{thm}{Theorem}[section]
\crefname{thm}{Thm.}{Thms.} % singular and plural forms of label

\newtheorem{cor}[thm]{Corollary}
\crefname{cor}{Cor.}{Cors.} % singular and plural forms of label

\newlist{enumthm}{enumerate}{1} % set up a dedicated enumeration env.
\setlist[enumthm]{label=\upshape(\alph*),ref=\upshape\thethm(\alph*)}
\crefalias{enumthmi}{thm} % alias 'enumthmi' counter to 'thm'

\newlist{enumcor}{enumerate}{1} % set up a second dedicated enumeration env.
\setlist[enumcor]{label=\upshape(\alph*),ref=\upshape\thecor(\alph*)}
\crefalias{enumcori}{cor} % alias 'enumcori' counter to 'cor'


\begin{document}
\setcounter{section}{1} % just for this example

\begin{thm}\label{Thm:One}
The following properties hold:
\begin{enumthm}
    \item\label{Thm:One:1} \(1>0\)
    \item\label{Thm:One:2} \(0<1\)
\end{enumthm}
\end{thm}

\begin{cor}\label{Thm:Two}
The following properties hold as well:
\begin{enumcor}
    \item\label{Thm:Two:1} \(2>1\)
    \item\label{Thm:Two:2} \(1<2\)
\end{enumcor}
\begin{proof}
\ref{Thm:Two:1} follows from \ref{Thm:One:1} by adding 1 on both sides. Similarly, \cref{Thm:Two:2} follows from \cref{Thm:One:2}.
\end{proof}
\end{cor}

\end{document} 

相关内容