tcolorbox – Newtcbtheorem vs Newtheorem: Resolving Counter Issues Efficiently

counterstcolorboxtheorems

I have a problem with counters: the newtcbtheorem's one conflits with newtheorem's one.
How could I solv my problem? Thank you so much.
I hope my question clear.

\PassOptionsToPackage{svgnames, x11names}{xcolor}
\documentclass[openany]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage[svgnames,x11names]{xcolor}
\usepackage{amscd}
\usepackage{color}
\usepackage{stix}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{imakeidx}
\usepackage[tight, italian]{minitoc}
\usepackage[most]{tcolorbox}
\tcbuselibrary{breakable,theorems,skins}
\usepackage[colorlinks=true]{hyperref}
\hypersetup{pdfstartview=FitH}
\usepackage{cleveref}

% New Exercises

\newtcbtheorem[number within=subsection] {exercise}{Esercizio}{%
enhanced, colback=red!5!white, colframe=gray, sharpish corners, breakable, fonttitle=\bfseries, coltitle=green, separator sign={\ ---},#1, description delimiters={$\bigl($}{$\bigr)$}}{ex}

\newtcbtheorem[use counter from=exercise] {solex}{}{enhanced, colback=red!5!white, colframe=gray, sharpish corners, breakable, separator sign none, fonttitle=\bfseries, coltitle=green, #1}{solex}

\newtheorem{esercizio}{Esercizio}[subsection]

% Per le soluzioni

\tcbset{exercise ref/.initial=,}

\newtcbtheorem[number within=subsection]{SolEx}{Soluzione dell'\Exref{\pgfkeysvalueof{/tcb/exercise ref}}}{enhanced, colback=red!5!white, colframe=black, sharpish corners, breakable, separator sign none, fonttitle=\bfseries, coltitle=orange}{ex}

% Nuove ref

\newcommand{\Exref}[1]{\textcolor{red}{Esercizio} \ref{ex:#1}}

\begin{document}

\Large

\begin{exercise}{Esercizio d'esempio}{EX1}

\end{exercise}

\begin{SolEx*}[exercise ref={EX1}]

\end{SolEx*}

\begin{esercizio}

\end{esercizio}

Problem with counter: I'd like this\\
\textbf{Esercizio 0.0.0.2}

\end{document}

Best Answer

source2e.pdf says

enter image description here

So you have to put tcb-environment exercise's counter into the first optional parameter, which is tcb@cnt@exercise.

Using

\makeatletter
\newtheorem{esercizio}[tcb@cnt@exercise]{Esercizio}
\makeatother

instead of

\newtheorem{esercizio}{Esercizio}[subsection]

will work.

enter image description here

Related Question