I am trying to use tcolorboxes to highlight exercises in a math tutorial (book document class) for a course I teach. I use a \label in the box hoping to reference the exercise by number in the solutions manual. However, the label does not "attach" to the colorbox; instead it returns the section number. Thus, Exercise "17" is referenced as "2.2.3" because that is the chapter section it is in. This behavior is different to that of a normal LaTeX environment and I am going in circles trying to resolve the issue. The tcolorbox manual is very detailed, but I cannot sort out this (seemingly) simple issue.
What I want to accomplish is this:
(1) create a box and put a \label{mylabel} in it.
(2) \ref{mylabel} returns the box (exercise) number.
(3) I want to reset the box numbering at the start of each chapter, with no "cross-talk" between the similarly numbered boxes in each chapter.
I hope somebody can help restore my sanity and reveal the secret to doing this!
Here is a MWE:
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{most}
\begin{document}
\section{Introduction}
This document contains labeled tcolorboxes in Section \ref{sec:example}. The first box is numbered as \ref{box:label1}.
\section{An Example Section}
\label{sec:example}
\newcounter{myboxcounter}
\newtcolorbox[auto counter]{mybox}[1][]{
enhanced,
colback=blue!5!white,
colframe=blue!75!black,
fonttitle=\bfseries,
before title={\refstepcounter{myboxcounter}},
title={Exercise~\arabic{myboxcounter}\ifx#1\empty\else\ (#1)\fi},
fonttitle=\bfseries,
breakable
}
\begin{mybox}[]
\label{box:label1}
This is box \ref{box:label1}.
\end{mybox}
\begin{mybox}[]
\label{box:label2}
This is box \ref{box:label2}.
\end{mybox}
\subsection{An Example Section}
\begin{mybox}[]
\label{box:label3}
This is box \ref{box:label3}. The first two are boxes \ref{box:label1} and \ref{box:label2}.
\end{mybox}
\end{document}


