![引用定理环境中的列表项](https://linux22.com/image/398799/%E5%BC%95%E7%94%A8%E5%AE%9A%E7%90%86%E7%8E%AF%E5%A2%83%E4%B8%AD%E7%9A%84%E5%88%97%E8%A1%A8%E9%A1%B9.png)
我正在尝试实现和定制解决方案允许在定理环境中进行交叉引用。
以下尝试是从我的论文模板中提取的(因此某些包可能不会用到,但对我来说是必要的)。
\documentclass[11pt,a4paper,twoside,final]{book}
\usepackage[utf8]{inputenc}
\usepackage[IL2]{fontenc}
\usepackage[czech]{babel}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{thmtools}
\usepackage{mathtools}
\usepackage{hyperref}
\declaretheoremstyle[bodyfont=\slshape]{slshape}
\declaretheorem[style=slshape,name=Theorem,numberwithin=chapter]{thm}
\declaretheorem[style=slshape,name=Lemma,numberlike=thm]{lem}
\usepackage{enumitem}
\newlist{thmlist}{enumerate}{1}
\setlist[thmlist]{label=(\roman{thmlisti}),noitemsep}
\usepackage[capitalize]{cleveref}
\newcounter{listthm}
\newcounter{listlem}
\Crefname{thm}{Theorem}{Theorems}
\Crefname{lem}{Lemma}{Lemmas}
\Crefname{listthm}{Theorem}{Theorems}
\Crefname{listlem}{Lemma}{Lemmas}
\addtotheorempostheadhook[thm]{\crefalias{thmlisti}{listthm}}
\addtotheorempostheadhook[lem]{\crefalias{thmlisti}{listlem}}
\creflabelformat{listthm}{#2\thethm.#1#3}
\creflabelformat{listlem}{#2\thethm.#1#3}
\begin{document}
\chapter{Peek-a-boo}
\begin{thm}\label{thm:A}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:A1}
\item Statment 2\label{thm:A2}
\end{thmlist}
\end{thm}
\begin{lem}\label{thm:B}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:B1}
\item Statment 2\label{thm:B2}
\end{thmlist}
\end{lem}
\begin{thm}\label{thm:C}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:C1}
\item Statment 2\label{thm:C2}
\end{thmlist}
\end{thm}
\begin{thm}\label{thm:D}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:D1}
\item Statment 2\label{thm:D2}
\end{thmlist}
\end{thm}
\begin{enumerate}
\item That's how I refer to theorems and their like: \cref{thm:A}, \cref{thm:B} and \cref{thm:C}.
\item Referring to parts of theorems works like a charm with \verb+\cref{•}:+
\begin{itemize}
\item \cref{thm:A1}
\item \cref{thm:B1}
\item \cref{thm:C}
\item \cref{thm:C1}
\item \cref{thm:C2}
\end{itemize}
\item If I use \verb+\ref{•}+ to refer to a part of a theorem \LaTeX{} returns \ref{thm:A1} and \ref{thm:B2} and \ref{thm:A}.
\end{enumerate}
\newpage
aaa
\end{document}
所有链接都指向它们应该指向的部分,但计数器存在问题(我猜?)。也就是说,以下cref
所有
\cref{thm:A1}
\cref{thm:B1}
\cref{thm:C1}
\cref{thm:C2}
打印最后一条定理的编号。那么自然而然的问题是,如何/修复那里?
答案1
您可以使用以下内容(我来到了同样的结论正如 Ulrike Fischer 所说:原始代码不从标签中获取计数器,而是使用调用时当前的计数器thm
值。 可以在与定理子项相关的命令中设置参考文献中使用的定理编号。thm
\cref
ref=\thethm.(\roman{thmlisti})
\setlist[thmlist]{...}
\documentclass{book}
\usepackage{amsmath,amsthm}
\usepackage{thmtools}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage[capitalize]{cleveref}
\declaretheoremstyle[bodyfont=\slshape]{slshape}
\declaretheorem[style=slshape,name=Theorem,numberwithin=chapter]{thm}
\declaretheorem[style=slshape,name=Lemma,numberlike=thm]{lem}
\newlist{thmlist}{enumerate}{1}
\setlist[thmlist]{label=(\roman{thmlisti}),
ref=\thethm.(\roman{thmlisti}),
noitemsep}
\Crefname{thm}{Theorem}{Theorems}
\Crefname{lem}{Lemma}{Lemmas}
\Crefname{listthm}{Theorem}{Theorems}
\Crefname{listlem}{Lemma}{Lemmas}
\addtotheorempostheadhook[thm]{\crefalias{thmlisti}{listthm}}
\addtotheorempostheadhook[lem]{\crefalias{thmlisti}{listlem}}
\begin{document}
\chapter{Peek-a-boo}
\begin{thm}\label{thm:A}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:A1}
\item Statment 2\label{thm:A2}
\end{thmlist}
\end{thm}
\begin{lem}\label{thm:B}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:B1}
\item Statment 2\label{thm:B2}
\end{thmlist}
\end{lem}
\begin{thm}\label{thm:C}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:C1}
\item Statment 2\label{thm:C2}
\end{thmlist}
\end{thm}
\begin{thm}\label{thm:D}
Assumptions
\begin{thmlist}
\item Statment 1\label{thm:D1}
\item Statment 2\label{thm:D2}
\end{thmlist}
\end{thm}
\begin{enumerate}
\item That's how I refer to theorems and their like: \cref{thm:A},
\cref{thm:B} and \cref{thm:C}.
\item Referring to parts of theorems works like a charm with \verb|\cref|:
\begin{itemize}
\item \cref{thm:A1}
\item \cref{thm:B1}
\item \cref{thm:C}
\item \cref{thm:C1}
\item \cref{thm:C2}
\end{itemize}
\item If I use \verb|\ref| to refer to a part of a theorem, \LaTeX\ prints
\ref{thm:A1}, \ref{thm:B2} and \ref{thm:A}.
\end{enumerate}
\end{document}
答案2
@frougon 答案的变体,使用:
- 列表
enumerate
本身,而无需定义thmlist
(这样就不必修改定理中的所有列表环境), etoolbox
和它的\AtBeginEnvironment
钩子而不是来自的钩子thmtools
(这样解决方案就不依赖于特定的定理包——这里我使用ntheorem
,并且我也用它进行了测试amsthm
)。
\documentclass{book}
\usepackage[margin=2cm]{geometry}
\usepackage{enumitem}
\usepackage[hyperref]{ntheorem}
\usepackage{hyperref}
\usepackage[capitalize]{cleveref}
\usepackage{etoolbox}
\theorembodyfont{\itshape}
\newtheorem{thm}{Theorem}[chapter]
\newtheorem{lem}[thm]{Lemma}
\Crefname{thm}{Theorem}{Theorems}
\Crefname{thmenumi}{Theorem}{Theorems}
\AtBeginEnvironment{thm}{%
\crefalias{enumi}{thmenumi}%
\setlist[enumerate,1]{
label={\textit{(\roman*)}},
ref={\thethm.(\roman*)}
}%
}
\Crefname{lem}{Lemma}{Lemmas}
\Crefname{lemenumi}{Lemma}{Lemmas}
\AtBeginEnvironment{lem}{%
\crefalias{enumi}{lemenumi}%
\setlist[enumerate,1]{
label={\textit{(\roman*)}},
ref={\thelem.(\roman*)}
}%
}
\begin{document}
\chapter{Peek-a-boo}
\begin{thm}\label{thm:A}
Assumptions
\begin{enumerate}
\item Statment 1\label{thm:A1}
\item Statment 2\label{thm:A2}
\end{enumerate}
\end{thm}
\begin{lem}\label{thm:B}
Assumptions
\begin{enumerate}
\item Statment 1\label{thm:B1}
\item Statment 2\label{thm:B2}
\end{enumerate}
\end{lem}
\begin{thm}\label{thm:C}
Assumptions
\begin{enumerate}
\item Statment 1\label{thm:C1}
\item Statment 2\label{thm:C2}
\end{enumerate}
\end{thm}
\begin{thm}\label{thm:D}
Assumptions
\begin{enumerate}
\item Statment 1\label{thm:D1}
\item Statment 2\label{thm:D2}
\end{enumerate}
\end{thm}
\begin{enumerate}
\item \label{item:1}
That's how I refer to theorems and their like: \cref{thm:A},
\cref{thm:B} and \cref{thm:C}.
\item Referring to parts of theorems works like a charm with \verb|\cref|:
\begin{itemize}
\item \cref{thm:A1}
\item \cref{thm:B1}
\item \cref{thm:C}
\item \cref{thm:C1}
\item \cref{thm:C2}
\end{itemize}
\item If I use \verb|\ref| to refer to a part of a theorem, \LaTeX\ prints
\ref{thm:A1}, \ref{thm:B2} and \ref{thm:A}.
\item Referring to items outside theorems (like \cref{item:1}) works as usual.
\end{enumerate}
\end{document}