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.
Best Answer
Use the
label
key for thetcolorbox
:In my example, I used
so the label has to be passed as an optional argument:
but you could also say
and then simply
Or you could make this a mandatory argument.
Remark
The
theorems
library fromtcolorbox
allows one to useand then
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.