2

I have a large document in which I've been using a lot of custom mdframed theorem environments defined using \newmdtheoremenv, for example

\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{myshadedthm}{backgroundcolor=gray!20,linewidth=0pt,innerleftmargin=1ex,innerrightmargin=0.5ex}
\newmdtheoremenv[style=myshadedthm]{thm}[prop]{\protect\theoremname}

They're used with an optional title argument as in

\begin{thm}[Title]
    Body
\end{thm}  

I would like to switch to using tcolorbox's \newtcbtheorem command for these environments, e.g.

\usepackage[most]{tcolorbox}
\newtcbtheorem{thm}{Theorem}{colback=gray!20,fonttitle=\bfseries}{th}

The problem is that this environment has two required arguments

\begin{thm}{Title}{Label}
    Body
\end{thm}  

which means I can't simply replace one definition with the other in the preamble. I can't figure out how to redefine the environment so that it can take one optional title argument and then feed it to the tcolorbox environment. It would be nice to make the label argument optional as well so I still have the option to use it, but without breaking existing code. Can anyone help?

1
  • Are you able to provide working code?
    – cfr
    Apr 3 at 5:32

2 Answers 2

1

The underlying command is \NewDocumentCommand so the inflexibility isn't really necessary (and not found in \NewTColorBox which these commands invoke).

The safest way is probably to define thm as a simple wrapper around an inner theorem environment, which can function in the usual way. You could avoid this by hacking something directly (as I did initially), but only by relying on internal expl3 macros. Since that's exactly the sort of thing you aren't meant to do in LaTeX 3 (and the cause of much of the packaging chaos in LaTeX 2e), you would have no grounds for complaint if an update broke the lot. And, after all, the whole point is backwards compatibility for existing documents, so breaking them would not seem the best option.

\documentclass[]{standalone}

\usepackage[most]{tcolorbox}

\NewTcbTheorem{ithm}{Theorem}{colback=gray!20,fonttitle=\bfseries}{th}
\NewDocumentEnvironment {thm} { O{} }
{%
  \begin{ithm}{#1}{}%
}{%
  \end{ithm}%
}

\begin{document}
\begin{thm}
    Body
\end{thm}  
\begin{thm}[Optional Title]
    Body
\end{thm}  
\end{document}

[Image omitted due to Okular bug but the first one has 'Theorem 1' as the title and the second 'Theorem 2: Optional Title'.]

0

I'd not use \newtbctheorem or similar: the syntax for the environment is clumsy, not to mention cross references.

\documentclass{article}
\usepackage[many]{tcolorbox}

\newtheorem{thm}{Theorem}
\tcolorboxenvironment{thm}{colback=gray!20}

\begin{document}

\begin{thm}\label{A}
Pigs can fly.
\end{thm}  

\begin{thm}[Interesting]\label{B}
Pigs can't fly.
\end{thm}  

\ref{A} and \ref{B}

\end{document}

enter image description here

If you have several theorem-like environments to be treated the same, you can avoid code duplication.

\documentclass{article}
\usepackage[many]{tcolorbox}

\newtheorem{thm}{Theorem}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{cor}[thm]{Corollary}
\newtheorem{prop}[thm]{Proposition}

\ExplSyntaxOn
\NewDocumentCommand{\tcbwrap}{mm}
 {
  \clist_map_inline:nn { #1 } { \tcolorboxenvironment{##1}{#2} }
 }
\ExplSyntaxOff
\tcbwrap{thm,lem,cor,prop}{colback=gray!20}

\begin{document}

\begin{thm}\label{A}
Pigs can fly.
\end{thm}

\begin{lem}
Whatever.
\end{lem}

\begin{thm}[Interesting]\label{B}
Pigs can't fly.
\end{thm}

\begin{cor}
Contradiction!
\end{cor}

\ref{A} and \ref{B}

\end{document}

enter image description here

Adjust the options. Setting fonttitle is not necessary. Use most only if really needed; usually many is sufficient.

3
  • What makes you say not to use tcolorbox? I'm using mdframed now but the page breaks are completely haywire and it looks like tcolorbox is supposed to deal with them better. Apr 3 at 14:51
  • @level1807 My answer does use tcolorbox! I recommend not using \newtcbtheorem unless you want fancy titles separated from the body. If you just want to frame a standard theorem, use \tcolorboxenvironment like I did.
    – egreg
    Apr 3 at 14:56
  • ohh I see, that's good to know, thank you Apr 3 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .