3

For the ordinary theorem environments in LaTeX, amslatex, etc., you can easily set the theorem number, for instance by temporarily setting the appropriate counters, or by redefining the functions that create the label, etc.

Do the tcolorbox theorem environments created with \newtcbtheorem have the functionality to reset the number on a theorem.

Here's a minimal example. What would I need to do to make the theorem in this document "Theorem 7.9"?

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem{Theorem}{Theorem}{}{Th}
\begin{document}
\begin{Theorem}{}{}
  We have $2 + 2 = 4$.
\end{Theorem}
\end{document}
6

As far as I know the tcolorbox package does not have a easy way to manipulate the counter value other than using the code= key with

code={\setcounter{\tcbcounter}{8}} where \tcbcounter is replaced by the real counter name of the box used.

With number within=section an automatic reset of the theorem counter within sections can be achieved.

enter image description here

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem[number within=section]{Theorem}{Theorem}{}{Th}


\begin{document}

\setcounter{section}{6}
\section{My nice section}
\begin{Theorem}[code={\setcounter{\tcbcounter}{8}}]{}{}
  We have $2 + 2 = 4$.
\end{Theorem}
\end{document}

Update

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem[number within=section]{Theorem}{Theorem}{}{Th}

\makeatletter
\newcommand{\providetcbcountername}[1]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    --undefined--%
  }{%
    tcb@cnt@#1%
  }
}

\newcommand{\settcbcounter}[2]{%
  \@ifundefined{c@tcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \setcounter{tcb@cnt@#1}{#2}%
}

\newcommand{\displaytcbcounter}[1]{% Wrapper for \the...
  \@ifundefined{thetcb@cnt@#1}{%
    \GenericError{Error}{counter name #1 is no tcb counter }{}{}%
  }{%
    \csname thetcb@cnt@#1\endcsname% 
  }%
}


\begin{document}

\setcounter{section}{6}
\section{My nice section}

\settcbcounter{Theorem}{8}

The counter for theorem is \providetcbcountername{Theorem} and it has the value of \displaytcbcounter{Theorem}


\begin{Theorem}{}{}
  We have $2 + 2 = 4$.
\end{Theorem}
\end{document}
  • OK, thanks. That works. It appears I can't access tcbcounter from outside the environment. Is that so? If so, how does tcbtheorem achieve that? – RMM Jan 19 '17 at 19:53
  • @RMM: You can access the counter name by looking into the .log file -- it reveals how tcolorbox names the counter. If the counter is named foo, the tcb name is tcb@cnt@foo. I've provided some wrappers to access the counter outside of tcolorbox environments, see the update at the end of my answer please.The \thetcbcounter and \tcbcounter macros are defined only for the current the environment and are not accessible from outside – user31729 Jan 19 '17 at 20:24
  • Many thanks. This fills the hole in my ability to use tcbtheorem. – RMM Jan 28 '17 at 5:23
  • @RMM: Alright, please consider to accept one of the given answers here then – user31729 Jan 28 '17 at 12:33
  • @FaouziBellalouna: Please ask a proper question, not a in row of comments – user31729 Apr 14 '18 at 19:27
2

You can use the *-variant:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\providecommand\theoremnumber{}
\newtcbtheorem{Theorembase}{Theorem \theoremnumber}{}{Th}
\newenvironment{Theorem}[1]
 {\renewcommand{\theoremnumber}{#1}\begin{Theorembase*}}
 {\end{Theorembase*}}

\begin{document}

\begin{Theorem}{7.9}{}
We have $2 + 2 = 4$.
\end{Theorem}

\end{document}

The second argument for \begin{theorem} is the optional title like for normal tcb theorems.

enter image description here

  • 1
    I see. Essentially you are wresting control of the numbering scheme from tcolorbox and replacing it with \theoremnumber. For a one off, that's fine, but it looks like you're tossing out the ability to use references to the theorem using \ref or similar systems. – RMM Jan 19 '17 at 19:33
  • @RMM If you number theorems manually, you don't need automatic cross references. – egreg Jan 19 '17 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.