GeneralNumbering of tcolorbox

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
noob2000
Posts: 3
Joined: Sun Oct 22, 2023 11:44 pm

Numbering of tcolorbox

Post by noob2000 »

Hello, friends! This little LaTeX-code works correctly and the reference to the Pythagorean theorem appears in the text as Reference to Pythagorean theorem: theorem 2.1:

\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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Numbering of tcolorbox

Post by Bartman »

Please use the tags intended for multiline code. Apart from a more readable highlighting, it can be tested in the same browser tab without having to copy anything first.

It may not solve the problem, but in the package manual the label is set by an option of the same name.

An environment created using the theorems library uses a required argument but still supports the option. Why don't you just use the library instead of trying to replicate what it already offers?
noob2000
Posts: 3
Joined: Sun Oct 22, 2023 11:44 pm

Numbering of tcolorbox

Post by noob2000 »

Bartman wrote:Please use the tags intended for multiline code. Apart from a more readable highlighting, it can be tested in the same browser tab without having to copy anything first.
Well, I used tags "LaTeX" and "/LaTeX" for latex-code. Do I need here some other tags? Well, let me try Code and \Code tags. The firs part:

Code: Select all

\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}
and the second part:

Code: Select all

\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}

Bartman wrote:It may not solve the problem, but in the package manual the label is set by an option of the same name.
I have already used it in the code. Or you mean something else?
Bartman wrote:An environment created using the theorems library uses a required argument but still supports the option. Why don't you just use the library instead of trying to replicate what it already offers?
The point is not in theorems, but in the use of tcolorboxes, their numbering with the ability to build correct lrefs.
Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Numbering of tcolorbox

Post by Bartman »

An example based on yours

Code: Select all

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\usepackage{cleveref}% added

\newtcolorbox[
  auto counter, 
  number within=section,
]{bbb}[2][]{
  enhanced, 
  coltitle=black, 
  fonttitle=\bfseries,
  attach title to upper={\ },
  label type=theorem,
  title=Theorem~\thetcbcounter: (#2), #1
}

\begin{document}
\section{S1} \subsection{SS1} \subsection{SS2}
\section{S2} \subsection{SS1} \subsection{SS2}

\begin{bbb}[label=teo1]{Pythagorean theorem}
\dots{} text of theorem 2.1~\dots
\end{bbb}

\begin{bbb}[label=teo2]{Abel's theorem}
\dots{} text of theorem 2.2~\dots
\end{bbb}

\begin{bbb}[label=teo3]{Vieta's formulas}
\dots{} text of theorem 2.3~\dots
\end{bbb}

Reference to Pythagorean theorem: \cref{teo1}
\end{document}
and one with the library mentioned

Code: Select all

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\usepackage{cleveref}% added

\newtcbtheorem[number within=section]{bbb}{Theorem}{
  coltitle=black, 
  fonttitle=\bfseries,
  attach title to upper={\ },
  label type=theorem
}{th}

\begin{document}
\section{S1} \subsection{SS1} \subsection{SS2}
\section{S2} \subsection{SS1} \subsection{SS2}

\begin{bbb}{(Pythagorean theorem)}{teo1}
\dots{} text of theorem 2.1~\dots
\end{bbb}

\begin{bbb}{(Abel's theorem)}{teo2}
\dots{} text of theorem 2.2~\dots
\end{bbb}

\begin{bbb}{(Vieta's formulas)}{teo3}
\dots{} text of theorem 2.3~\dots
\end{bbb}

Reference to Pythagorean theorem: \cref{th:teo1}
\end{document}
noob2000
Posts: 3
Joined: Sun Oct 22, 2023 11:44 pm

Numbering of tcolorbox

Post by noob2000 »

Bartman, I really want to thank you for your help!
Post Reply