Tcolorbox Theorems – Using Environments with Tcolorbox for Referencing Macros

cross-referencingenvironmentsmacrostcolorboxtheorems

I have a large textbook written and I wish to add color boxes around my theorems, lemmas, etc without changing my main text too much. Currently, each theorem environment is defined in the usual way, e.g.,

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem*{proposition*}{Proposition}

To add color boxes, I could of course use tcolorbox as in this answer. My problem is that tcolorbox's theorems are defined like

\begin{theorem}{theorem name}{reference}

whereas my 1000-page textbook define theorems using

\begin{theorem}{theorem name}\label{reference}

So my question is:

Is there any way of getting around this. I.e., redefining the theorem-environment so that I would still be able to reference theorems using the usual \label{reference}?

Many thanks in advance!

Best Answer

If you accept to rename your theorems to something different, you can use tcolorboxenvironment to change theorems aspect and keep previous labeling mechanism.

\documentclass{article}

\usepackage[skins]{tcolorbox}
\usepackage{amsmath, amsthm}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem*{proposition*}{Proposition}

\newenvironment{mytheorem}{\begin{theorem}}{\end{theorem}}

\tcolorboxenvironment{mytheorem}{colback=blue!30, colframe=blue!70!black}

\begin{document}

\section{One}

\begin{mytheorem}{One theorem}\label{OneTheorem}
some words
\end{mytheorem}

Reference to theorem~\ref{OneTheorem}

\end{document}

enter image description here

Update: muzimuzhi's simpler solution

it seems that any previous existing environment can be tcbcolorized, we don't need to rename it as I did in original answer.

\documentclass{article}

\usepackage[skins]{tcolorbox}
\usepackage{amsmath, amsthm}

\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem*{proposition*}{Proposition}

%\newenvironment{mytheorem}{\begin{theorem}}{\end{theorem}}

\tcolorboxenvironment{theorem}{colback=blue!30, colframe=blue!70!black}

\begin{document}

\section{One}

\begin{theorem}{One theorem}\label{OneTheorem}
some words
\end{theorem}

Reference to theorem~\ref{OneTheorem}

\end{document}

enter image description here

Related Question