\documentclass[dvipsnames]{article}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\newtcolorbox[auto counter, number within=section]{bbb}[1][]{
enhanced, coltitle=black, fonttitle=\bfseries,
title=Theorem~\thesection.\arabic{\tcbcounter} #1,
attach title to upper={\ },
label={theorem.\thesection.\arabic{\tcbcounter}},
nameref={\thesection.\arabic{\tcbcounter}}
}
\begin{document}
\section{S1} \subsection{SS1} \subsection{SS2}
\section{S2} \subsection{SS1} \subsection{SS2}
\begin{bbb}[(Pythagorean theorem)]\label{teo1}
...text of theorem 2.1...
\end{bbb}
Reference to Pythagorean theorem: theorem \nameref{teo1}
\end{document}
But if we add two more theorems, then the same reference already begins to appear in the text as Reference to Pythagorean theorem: theorem 2.3:
\documentclass[dvipsnames]{article}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\newtcolorbox[auto counter, number within=section]{bbb}[1][]{
enhanced, coltitle=black, fonttitle=\bfseries,
title=Theorem~\thesection.\arabic{\tcbcounter} #1,
attach title to upper={\ },
label={theorem.\thesection.\arabic{\tcbcounter}},
nameref={\thesection.\arabic{\tcbcounter}}
}
\begin{document}
\section{S1} \subsection{SS1} \subsection{SS2}
\section{S2} \subsection{SS1} \subsection{SS2}
\begin{bbb}[(Pythagorean theorem)]\label{teo1}
...text of theorem 2.1...
\end{bbb}
\begin{bbb}[(Abel's theorem)]\label{teo2}
...text of theorem 2.2...
\end{bbb}
\begin{bbb}[(Vieta's formulas)]\label{teo3}
...text of theorem 2.3...
\end{bbb}
Reference to Pythagorean theorem: theorem \nameref{teo1}
\end{document}
How can this be fixed so that the ref displays correctly?
I have already tried so many things, but all in vain.