Inspired by this great answer, I'm trying to typeset my definitions using tcolorbox. My document contains hundreds of definitions which follow the pattern:
\begin{definition}
% ...
\label{something:meaningful}
\end{definition}
My problem is that I haven't found a way to define a definition environment based on tcolorbox where the labels work as expect. To demonstrate the problem, I have created a MWE:
\documentclass[12pt]{book}
\usepackage[paperheight=100in]{geometry} % just to allow taking a screen shot...
\usepackage[most]{tcolorbox}
% this is my current "definition" environment
\newtcolorbox[auto counter,number within=chapter]{definition}{
enhanced,
breakable,
fonttitle=\sc,
title={Definition \thetcbcounter}
}
\let\clearpage\relax % also to allow a screenshot
\begin{document}
\chapter{First Chapter}
\section{Section 1}
\begin{definition}
A first definition. Labeled \texttt{def:A}.
\label{def:A}
\end{definition}
\section{Section 2}
\begin{definition}
Another definition. Labeled \texttt{def:B}.
\label{def:B}
\end{definition}
\subsection{With a Subsection}
\begin{definition}
Another definition -- this time nested. Labeled \texttt{def:C}.
\label{def:C}
\end{definition}
\chapter{Second Chapter}
\begin{definition}
Definition in the second chapter. Labeled \texttt{def:D}.
\label{def:D}
\end{definition}
Judging from the definition headers, it looks like the per-chapter-counter seems to work.
But lets see what happens if we reference the definitions.
\begin{itemize}
\item \texttt{\textbackslash ref\{def:A\}} refers to Definition \ref{def:A}.
\item \texttt{\textbackslash ref\{def:B\}} refers to Definition \ref{def:B}.
\item \texttt{\textbackslash ref\{def:C\}} refers to Definition \ref{def:C}.
\item \texttt{\textbackslash ref\{def:D\}} refers to Definition \ref{def:D}.
\end{itemize}
\end{document}
So I'm using an auto-counter on a per chapter basis. This counter itself also seem to work, since the header of my definitions have a correct per-chapter numbering. The problem is that referring to the labels does not give the same number. This is the output of the above MWE:

In fact it looks like the labels simply refer to the surrounding chapter/section. Is there a way to make the labels consistent with the definition number?
I have discovered that there is an optional label argument for environments defined by newtcolorbox. However, I haven't found a way to use it in my case. I guess it only works for environments that have a mandatory argument, and I don't want to break compatibility to all my existing definitions following the above pattern.



tcolorboxhandles the labels internally, but this will have to wait. Perhaps, in the meantime, someone else will provide a better alternative. – Gonzalo Medina Mar 04 '15 at 18:50newcbtheoremin the first place was just that I was trying to avoid refactoring all definitions/labels. – bluenote10 Mar 04 '15 at 18:57\namerefwith the correct name-the value of the title key, however. Is this correct? It did not work with my test. Maybe I am missing something. – Jonathan Komar Apr 15 '16 at 06:58