0

How can I create tcolorbox theorem environments with labelling via \label

I have some auto-generated code that looks like

\begin{thm}[Title with \(\LaTeX\)]
\label{label}    
Here is a theorem
\end{thm}

I want to render the theorem in a tcolorbox. I could use \newtheorem{thm}{Theorem} and the \tcolrboxenvironment{thm}{options} command to put a box around it, but I don't want to just put a box around the environment. I want greater control over how it's displayed. I'd like to use tcolorboxes to display the theorem, so it would appear as it does with something like the following (where of course I would have a more interesting style):

\usepackage[most]{tcolorbox}
\tcbset{mystyle/.style={}}
\newtcbtheorem{thm}{Theorem}{mystyle}{}
\newcommand{\thmautorefname}{Theorem}

\begin{thm}[label=label]{My theorem (x^2)}{} Here is a theorem \end{thm}

and reference the theorems with various ref commands such as

\nameref{label} with number \ref{label} and autoref \autoref{label}

A theorem environment with tcolorbox

The issue is that the environment created with \newtcbtheorem requires different syntax than the auto-generated code.

I have a way to do it by creating my own environments, but I feel like I'm reinventing many wheels to achieve the goal. I was hoping some mix of tcolorbox and amsthm could be used to do the job instead.

Here is a MWE of current solution where I create environments to wrap tcolorboxes. My actual solution is a little more complex since I made a macro similar to \newtheorem{thm}{Theorem} to create environments, with shared counters and then I feel I am starting to reinvent amsthm!

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{hyperref}

\tcbset{mystyle/.style={}}

\makeatletter \newcounter{thm} \newenvironment{thm}[1][] {\refstepcounter{thm} \protected@edef@currentlabelname{#1} \begin{tcolorbox}[title={Theorem @currentlabel{}: #1}, mystyle]} {\end{tcolorbox}} \newcommand{\thmautorefname}{Theorem} \makeatother

\begin{document}

\begin{thm}[My Theorem (x^2)] \label{label} Here is a theorem. \end{thm}

\nameref{label} with number \ref{label} and autoref \autoref{label} \end{document}

  • May be this helps: https://tex.stackexchange.com/q/231375/1952 – Ignasi Oct 25 '22 at 09:14
  • Thanks! I haven't tested it yet, but the answer of @xavierm02 seems to be the only one that will do what I want reliably. But it's quite a hack! I think it would be more maintainable for me to use my current solution. – Paul Bryan Oct 26 '22 at 00:45

0 Answers0