我喜欢使用amsthm
定理等,但为了强调它们的重要性,我喜欢将练习放在tcolorbox
es 中。我希望方框中的练习与我的定理共享相同的计数器。
我设法创建了一个exercise
可按预期和谐运行的环境cleveref
:
但是,我花了一段时间才意识到这一点,但出于某种原因,如果不同部分中的两个练习恰好以相同的数字结尾(例如来自第 X 部分的练习 X.2 和来自 Y 部分的练习 Y.2),那么做\cref{theExercise}
总是会指向第一个(即使我参考第二个)。
这是一个 MWE。
\documentclass{article}
\usepackage{amsmath,amsthm,lipsum}
\usepackage[most]{tcolorbox}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}
% Theorem Environments
\newtheorem{lemma}{Lemma}[section]
\newtheorem{definition}[lemma]{Definition}
% Exercise Environment
\makeatletter
\let\c@exercise\c@lemma
\def\p@exercise{\p@lemma}
\def\theexercise{\thelemma}
\makeatother
\crefname{exercise}{exercise}{exercises}
\newenvironment{exercise}[1][]{
\refstepcounter{exercise}\par\medskip
\begin{tcolorbox}[breakable, enhanced, colback=gray!7!white, parbox=false, drop fuzzy shadow]
\noindent {\textbf{Exercise~\theexercise #1}}
\rmfamily\par\medskip
}
{
\end{tcolorbox}
\medskip
}
\begin{document}
\section{The First Section}
\begin{definition}[Continuity]
\label{def:continuity}
$f\colon A\to B$ is continuous if for all $U \subseteq B$,
\[\text{$U$ is open in $B$} \implies \text{$f^{-1}(U)$ is open in $A$.} \]
\end{definition}
\begin{exercise}
\label{exercise:continuityEpsilonDelta}
Let $A\subseteq\mathbf R$. Show that $f\colon A\to\mathbf R$ is continuous in the sense of \cref{def:continuity} if and only if for all $a\in A$,
\[(\forall\epsilon > 0)(\exists\delta>0)(\forall x\in A)(0<|a-x|<\delta\implies |f(x)-f(a)|<\epsilon). \]
\end{exercise}
{\bfseries Notice here \verb|\theexercise| is \theexercise.}
\section{The Second Section}
\begin{lemma}[Handshaking Lemma]
\label{lemma:HS}
Let $G=(V,E)$ be a graph. Then
\[\sum_{v\in V} \deg(v) = 2|E|. \]
\end{lemma}
\begin{exercise}
\label{exercise:HS}
Prove that if $H=(V,E)$ is a hypergraph, then
\[\sum_{v\in V}\deg(v) = \sum_{e\in E}|e|. \]
You may use \cref{lemma:HS}.
\end{exercise}
{\bfseries Notice here \verb|\theexercise| is \theexercise.}
\pagebreak
\appendix
\section{Solutions to Exercises}
The solution to \cref{exercise:HS} is the following.
\begin{proof}
\lipsum[1]
\end{proof}
\end{document}
单击附录中的链接\cref{exercise:HS}
将带您进入第一个练习,尽管我引用了第二个练习。
我很感激任何对此提供的帮助。
答案1
定义一个适当的定理类环境。我尝试重现你使用的设置,尽管我不太喜欢它:空白空间太多了。
\documentclass{article}
\usepackage{amsmath,amsthm,lipsum}
\usepackage[most]{tcolorbox}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}
% Theorem Environments
\newtheorem{lemma}{Lemma}[section]
\newtheorem{definition}[lemma]{Definition}
\theoremstyle{definition}
\newtheorem{exerciseinner}[lemma]{Exercise}
\newenvironment{exercise}{\exerciseinner\mbox{}\par\bigskip}{\endexerciseinner}
\newcommand{\theexercise}{\theexerciseinner}
% Exercise Environment
\tcolorboxenvironment{exercise}{
breakable,
enhanced,
colback=gray!7!white,
parbox=false, drop fuzzy shadow
}
\begin{document}
\section{The First Section}
\begin{definition}[Continuity]\label{def:continuity}
$f\colon A\to B$ is continuous if for all $U \subseteq B$,
\[
\text{$U$ is open in $B$} \implies \text{$f^{-1}(U)$ is open in $A$.}
\]
\end{definition}
\begin{exercise}\label{exercise:continuityEpsilonDelta}
Let $A\subseteq\mathbf R$. Show that $f\colon A\to\mathbf R$ is
continuous in the sense of \cref{def:continuity} if and only if
for all $a\in A$,
\[
(\forall\epsilon > 0)(\exists\delta>0)
(\forall x\in A)(0<|a-x|<\delta\implies |f(x)-f(a)|<\epsilon).
\]
\end{exercise}
{\bfseries Notice here \verb|\theexercise| is \theexercise.}
\section{The Second Section}
\begin{lemma}[Handshaking Lemma]\label{lemma:HS}
Let $G=(V,E)$ be a graph. Then
\[
\sum_{v\in V} \deg(v) = 2|E|.
\]
\end{lemma}
\begin{exercise}\label{exercise:HS}
Prove that if $H=(V,E)$ is a hypergraph, then
\[
\sum_{v\in V}\deg(v) = \sum_{e\in E}|e|.
\]
You may use \cref{lemma:HS}.
\end{exercise}
{\bfseries Notice here \verb|\theexercise| is \theexercise.}
\pagebreak
\appendix
\section{Solutions to Exercises}
The solution to \cref{exercise:HS} is the following.
\begin{proof}
\lipsum[1]
\end{proof}
\end{document}
我更愿意\newtheorem{exercise}[lemma]{Exercise}
(而不是\newenvironment{exercise}
)得到
答案2
您可以使用可选参数,\label
如下所示:
\label[exercise]{exercise:HS}
这将为您提供文本中引用的预期行为。
为了获得更加自动化的解决方案,您可以使用\crefalias
如以下修改后的练习环境版本所示。
\newenvironment{exercise}[1][]{\crefalias{lemma}{exercise}
\refstepcounter{lemma}\par\medskip
\begin{tcolorbox}[breakable, enhanced, colback=gray!7!white, parbox=false, drop fuzzy shadow]
\noindent {\textbf{Exercise~\thelemma #1}}
\rmfamily\par\medskip
}
{
\end{tcolorbox}
\medskip
}
更多相关信息,请参阅手册的“6 覆盖交叉引用类型”一章cleveref
。