2

Here is a custom theorem environment that utilizes mdframed:

\documentclass{article}

\usepackage[margin=1in]{geometry}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{ifthen}

%theorembox style
\mdfdefinestyle{theorembox}{%
    leftmargin=2em,
    rightmargin=2em,
    innertopmargin=8pt,
    innerbottommargin=8pt,
    roundcorner=8pt,
    backgroundcolor=gray!15
}

%theorem
\newenvironment{theorem}[1][]{%
    \newcounter{thm}
    \setcounter{thm}{1}
    \newsavebox{\thmargbox}
    \savebox{\thmargbox}{#1}
    \begin{list}{}{%
        \setlength{\leftmargin}{0pt}
        \setlength{\rightmargin}{0pt}
    }       
        \item
        \begin{mdframed}[style=theorembox]
            {\large Theorem \arabic{thm}\quad\ifthenelse{\wd\thmargbox=0}{}{#1\par}}%
}{%     
        \end{mdframed}
    \end{list}
    \stepcounter{thm}
}

\begin{document}

\begin{theorem}[First Theorem] 
  If $a\sim b$, then $b\sim a$.
\end{theorem}

%\begin{theorem}[Second Theorem] 
%  If $a\sim b$ and $b\sim c$, then $a\sim c$.
%\end{theorem}

\end{document}

custom theorem environment

However, if the second theorem is applied, the following error is reported:

! LaTeX Error:  Command \c@thm already defined.

I think this may have more to do with my counter than mdframed, but any assistance would be appreciated.

2
  • move \newcounter{thm} outside the environment. Mar 19, 2022 at 19:50
  • @UlrikeFischer That fixes the reported error, thank you. But a second undisclosed thmargbox already defined error persists. Mar 19, 2022 at 23:56

1 Answer 1

1

You have to move \newcounter{thm} and to replace \setcounter{thm}{1} with \refstepcounter{thm}, removing \stepcounter{thm} at the end.

But there are much better ways to do what you want with tcolorbox and the standard theorem environments.

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

\newtheoremstyle{largebreak}
  {}% use the default space above
  {}% use the default space below
  {\normalfont}% body font
  {}% indent (0pt)
  {\large}% header font
  {}% punctuation
  {\newline}% break after header
  {\thmname{#1}\thmnumber{ #2}\thmnote{\quad #3}}% header spec

\theoremstyle{largebreak}
\newtheorem{theorem}{Theorem}
\tcolorboxenvironment{theorem}{
  colback=gray!15,
  boxrule=0.4pt,
}

\begin{document}

\begin{theorem}[First Theorem]
  If $a\sim b$, then $b\sim a$.
\end{theorem}

\begin{theorem}[Second Theorem]
  If $a\sim b$ and $b\sim c$, then $a\sim c$.
\end{theorem}

\begin{theorem}% no optional argument
$1+1=2$, no question.
\end{theorem}

\end{document}

enter image description here

You can get a similar result with mdframed:

\documentclass{article}
\usepackage{amsthm}
\usepackage[framemethod=tikz]{mdframed}

\newtheoremstyle{largebreak}
  {}% use the default space above
  {}% use the default space below
  {\normalfont}% body font
  {}% indent (0pt)
  {\large}% header font
  {}% punctuation
  {\newline}% break after header
  {\thmname{#1}\thmnumber{ #2}\thmnote{\quad #3}}% header spec

\theoremstyle{largebreak}
\newmdtheoremenv[
    leftmargin=2em,
    rightmargin=2em,
    innertopmargin=2pt,
    innerbottommargin=8pt,
    roundcorner=8pt,
    backgroundcolor=gray!15
]{theorem}{Theorem}

\begin{document}

\begin{theorem}[First Theorem] 
  If $a\sim b$, then $b\sim a$.
\end{theorem}

\begin{theorem}[Second Theorem] 
  If $a\sim b$ and $b\sim c$, then $a\sim c$.
\end{theorem}

\begin{theorem}% no optional argument
$1+1=2$, no question.
\end{theorem}

\end{document}

enter image description here

5
  • I'd like to use your tcolorbox solution, but it doesn't compile on my installation. LaTeX reports 238 errors, the first of which is the undefined control sequence \ExplSyntaxOn. Mar 19, 2022 at 23:48
  • 1
    Then you use probably a rather old tex system or didn't update properly. Mar 20, 2022 at 7:18
  • 1
    @steven_nevets I added the mdframed code, but you should upgrade your TeX system.
    – egreg
    Mar 20, 2022 at 10:46
  • @egreg I know you're not supposed to say thank you, but I can't thank you enough. I'm happy to learn how to do this properly with mdframed, and even happier to learn about tcolorbox. Mar 20, 2022 at 22:18
  • @steven_nevets You're welcome!
    – egreg
    Mar 20, 2022 at 22:20

You must log in to answer this question.

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