[Tex/LaTex] tcolorbox: \newtcbtheorem referencing

cleverefcross-referencingtcolorbox

Using \newtcbtheorem, how do we reference the created environment?

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{cleveref}
\crefname{exa}{example}{Example}
\Crefname{exa}{Example}{Example}
\newtcbtheorem[auto counter, number within = section]
{Example}{Exam\smash{p}le}{%                                                        
  breakable,
  fonttitle = \bfseries,
  colframe = blue!75!black,
  colback = blue!10
}{exa}
\begin{document}
\begin{Example}{Example}{exam}
  Some example
\end{Example}
\noindent
\Cref{exa:exam}
\end{document}

Using tcbmaketheorem, I used \newcounter{exa}, but I can't get referencing to work here.

Best Answer

You need to define the format for the new object; in this case, for tcb@cnt@Example, using something like

\crefformat{tcb@cnt@Example}{example~#2#1#3}
\Crefformat{tcb@cnt@Example}{Example~#2#1#3}

or any other format you want. A complete example:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{cleveref}

\makeatletter
\crefformat{tcb@cnt@Example}{example~#2#1#3}
\Crefformat{tcb@cnt@Example}{Example~#2#1#3}
\makeatother
\newtcbtheorem[auto counter, number within = section]
{Example}{Exam\smash{p}le}{%                                                        
  breakable,
  fonttitle = \bfseries,
  colframe = blue!75!black,
  colback = blue!10
}{exa}
\begin{document}
\begin{Example}{Example}{exam}
  Some example
\end{Example}
\noindent
\Cref{exa:exam} and~\cref{exa:exam}

\end{document}

enter image description here

The correct name to be used in \Crefformat, and \crefformat can be obtained from one of the warnings obtained in the original code using \Cref without the proper definitions:

LaTeX Warning: Cref reference format for label type `tcb@cnt@Example' undefined on input line 21.
Related Question