How to define labels within a tcolorbox?
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}
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.
Here is a way to make \label
work inside a tcolorbox
– but this is really dirty and only works under the assumption, that the \label
command immediately follows the \begin{definition}
command!
It works by "catching" the \label
token using xparse
s t
argument. To use xparse
we have to create the environment by hand and not by the means of the theorem
library.
An advantage of this ugly hack is the fact, that the autocomplete features of some tex(t)-editors should be able to find the labels defined this way, whereas the ones created with [label=def:test]
will not be found – at least "out-of-the-box".
Consider the following MWE:
\documentclass{article}
\usepackage{cleveref}
\usepackage{tcolorbox}
\tcbuselibrary{xparse,skins,breakable}
\tcbset{%
basestyle/.style={enhanced,breakable,
fonttitle=\scshape,
coltitle=white},
defstyle/.style={basestyle,colback=red!10!white},
theostyle/.style={basestyle,colback=blue!10!white},
}
\DeclareTColorBox[auto counter,number within=section,crefname={definition}{definitions}]{definition}{ o O{} t\label g }{%
defstyle,IfValueTF={#1}{title={Definition~\thetcbcounter\ (#1)}}{title=Definition~\thetcbcounter}, IfBooleanTF={#3}{label=#4}{},#2}
\begin{document}
\begin{definition}[My title]\label{def:test}
Foo.
\end{definition}
This was \cref{def:test}.
Unlabeled definition:
\begin{definition}[Cool Title][colback=orange!10!white]
Just a test.
\end{definition}
\end{document}
Output: