# Problem with Counters and Cleveref

I like using amsthm for my theorems, etc, but to highlight their importance, I like exercises to be in tcolorboxes. I want the exercises in the boxes to share the same counter as my theorems.

I managed to create an exercise environment which works as desired, harmoniously with cleveref:

But then, it took me a while to realise this, but for some reason, if two exercises in different sections happen to end in the same number (example exercise X.2 from section X and exercise Y.2 from section Y), then doing \cref{theExercise} will always point me to the first one (even if I refer to the second one).

Here is an 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}


Clicking the link of \cref{exercise:HS} in the appendix will take you to the first exercise, even though I reference the second.

I appreciate any assistance with this.

## 2 Answers

Define a proper theorem-like environment. I tried to reproduce the setup you're using, although I don't like it much: the blank space is excessive.

\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}


I'd much prefer \newtheorem{exercise}[lemma]{Exercise} (and no \newenvironment{exercise}) to get

• I agree with your stylistic choice. Thank you for this solution. – Luke Collins Apr 11 at 1:14

You could use the optional argument of \label as follows:

\label[exercise]{exercise:HS}

This will give you the expected beahviour of the reference in the text.

For a more automated solution, you could use \crefalias as shown in the following modified version of your exercise environment.

\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
}


More information on this can be found in chapter "6 Overriding the Cross-Reference Type" of the cleveref manual.