Environments – How to Define Labels Within a Tcolorbox

environmentstcolorbox

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:

output

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.

Best Answer

Use the label key for the tcolorbox:

\documentclass[12pt]{book}
\usepackage[most]{tcolorbox}

% this is my current "definition" environment
\newtcolorbox[auto counter,number within=chapter]{definition}[1][]{
  enhanced,
  breakable,
  fonttitle=\scshape,
  title={Definition \thetcbcounter},
  #1
}

\begin{document}

\chapter{First Chapter}
\section{Section 1}
\begin{definition}[label=def:A]
A first definition. Labeled \texttt{def:A}.
\end{definition}

\section{Section 2}
\begin{definition}[label=def:B]
Another definition. Labeled \texttt{def:B}.
\end{definition}

\subsection{Whit a Subsection}
\begin{definition}[label=def:C]
Another definition -- this time nested. Labeled \texttt{def:C}.
\end{definition}

\chapter{Second Section}
\begin{definition}[label=def:D]
Definition in the second chapter. Labeled \texttt{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}

enter image description here

In my example, I used

\newtcolorbox[auto counter,number within=chapter]{definition}[1][]{
  enhanced,
  breakable,
  fonttitle=\scshape,
  title={Definition \thetcbcounter},
  #1
}

so the label has to be passed as an optional argument:

\begin{definition}[label=<text>]
...
\end{definition}

but you could also say

\newtcolorbox[auto counter,number within=chapter]{definition}[1][]{
  enhanced,
  breakable,
  fonttitle=\scshape,
  title={Definition \thetcbcounter},
  label=#1
}

and then simply

\begin{definition}[<text>]
...
\end{definition}

Or you could make this a mandatory argument.

Remark

The theorems library from tcolorbox allows one to use

\newtcbtheorem[<init options>]{<name>}{<display name>}{<options>}{<prefix>}

and then

\begin{name}{}{<label>}
...
\end{name}

so one can say \ref{<prefix:<label>} to get cross-references. I didn't use this in the answer linked in the question since there no numbering (so no cross-referencing) was required.

Related Question