# Nice formatting for theorems

I'm writing an article with some theorems and I would like to use special formatting for the theorems - my idea is putting a grey line in the side of the theorem. Can you please show me how to do this?

Also, if you have other ideas for nice formatting for theorems, etc., I would like to hear it.

• Could you show an example of the sort of thing you want? The packages amsthm or ntheorem might achieve what you want. – Seamus Feb 14 '11 at 23:06
• Since you want to use LyX I'll only comment, that the thmtools package has features for what you want. – Carsten Thiel Feb 15 '11 at 9:59
• I went ahead and tried to create a custom theorem style using thmtools to fit the design Amir wants. I was not successful. I couldn't for the life of me figure out how to use the theoremdefiner. – Mikael Öhman Feb 15 '11 at 12:12
• @Mikael: you're not supposed to tap into theoremdefiner. Without having given a deeper look, giving preheadhook and postfoothook keys should be sufficient, similar to the way it's done for the shaded key. – Ulrich Schwarz Feb 26 '11 at 9:04
• @Mikael: for completeness' sake: \declaretheorem[% preheadhook={\begin{mdframed}[leftline=true,rightline=false,% topline=false,bottomline=false]},% postfoothook=\end{mdframed}% ]{theorem} (with linbreaks after each %.) – Ulrich Schwarz Feb 26 '11 at 9:16

I had a different question but maybe the answer from Marco Daniel is what you are searching for: Different colorcoded theorems. He proposed using the package mdframed.

Example Output from my thesis:

Not exactly what you want but this is an example by Donald Arseneau that uses framed package:

\documentclass{article}
\usepackage{color,amsmath}
\usepackage{framed}
\makeatletter
\newdimen\errorsize \errorsize=0.2pt
% Frame with a label at top
\newcommand\LabFrame[2]{%
\fboxrule=\FrameRule
\fboxsep=-\errorsize
\textcolor{FrameColor}{%
\fbox{%
\vbox{\nobreak
\begingroup
\hrule height \baselineskip
\nobreak
\vskip-\baselineskip
\endgroup
\vskip 0.5\FrameSep
\hbox{\hskip\FrameSep \strut
\textcolor{TitleColor}{\textbf{#1}}}%
\nobreak \nointerlineskip
\vskip 1.3\FrameSep
\hbox{\hskip\FrameSep
{\normalcolor#2}%
\hskip\FrameSep}%
\vskip\FrameSep
}}%
}}
\definecolor{FrameColor}{rgb}{0.25,0.25,1.0}
\definecolor{TitleColor}{rgb}{1.0,1.0,1.0}

\newenvironment{contlabelframe}[2][\Frame@Lab\ (cont.)]{%
% Optional continuation label defaults to the first label plus
\def\Frame@Lab{#2}%
\def\FrameCommand{\LabFrame{#2}}%
\def\FirstFrameCommand{\LabFrame{#2}}%
\def\MidFrameCommand{\LabFrame{#1}}%
\def\LastFrameCommand{\LabFrame{#1}}%
}{\endMakeFramed}
\newcounter{theorem}
\newenvironment{theorem}[1]{%
\par
\refstepcounter{theorem}%
\noindent\ignorespaces}
{\end{contlabelframe}}
\makeatother
\begin{document}
\begin{theorem}{Pythagoras}
This is a theorem
\end{theorem}
\end{document}


And This is what you get:

• tried copying this into LyX (in LaTeX preamble) and I get LaTeX error: command \c@theorem already defined. – Amir Rachum Feb 15 '11 at 16:47
• Do you use amsthm or any other theorem thing package, if you do, then firstly you need to delete or comment \newcounter{theorem}. – IRAN Feb 15 '11 at 17:08

Another possibility to decorate theorems is shown in the example above. The example is also shown in the new version 1.0 of mdframed (uploaded today)

\listfiles
\documentclass{article}
\usepackage{amsmath,tikz}
\usepackage[framemethod=default,
skipabove=\topskip,
skipbelow=\topskip,
]{mdframed}
\usepackage{lipsum}
\makeatletter

\makeatletter
\newcounter{theo}[section]
\@afterindentfalse
{\parindent \z@
\setlength{\parfillskip}{\z@ plus 1fil}%
\mdraggedtitle\nobreak%
\makebox[\linewidth][l]{%
\hspace*{-1\mdf@innerleftmargin@length}%
\rlap{\color{white}%
\hspace*{-1\mdf@middlelinewidth@length}%
\rule[\mdf@middlelinewidth@length]%
{\dimexpr\linewidth+1\mdf@innerleftmargin@length%
+\mdf@innerrightmargin@length
+2\mdf@middlelinewidth@length\relax}%
{\dimexpr\ht\strutbox+.3333em
\relax}%
}%
\rlap{\color{blue!20}%
\rule{\dimexpr\linewidth+\mdf@innerleftmargin@length%
+\mdf@innerrightmargin@length\relax}%
{\mdf@middlelinewidth@length}}%
\hspace*{-1\mdf@middlelinewidth@length}%
\tikz[remember picture,baseline]\node[,draw = none, text = black,
fill = blue!20,]
{\mdf@frametitlefont\strut Theorem~\thetheo#1};\relax%
}%
\par\kern.5\baselineskip}%

\newenvironment{theo}[1][]{%
\stepcounter{theo}%
\ifstrempty{#1}{\mdfsetup{frametitle={\strut}}}{\mdfsetup{frametitle={:~#1}}}%
\begin{mdframed}[innertopmargin=0pt,linecolor=blue!20,linewidth=2pt,topline=false,]%
}{\end{mdframed}}
\makeatother
\begin{document}
\section{First section}
Text
\begin{theo}[Theorem lipsum]
\lipsum[1]
\end{theo}

\begin{theo}
\lipsum[2]
\end{theo}
\end{document}


• I don't get this output with TeXLive 2014. The output I get is with the title and top line missing as shown here. – Peter Grill Oct 23 '14 at 7:08
• Yepp, me to - with miktex 2.9. – lukas.coenig Mar 12 '16 at 6:48
• This worked for me: \newcounter{theo}[chapter] \newenvironment{theo}[1]{% \stepcounter{theo}% \ifstrempty{#1}% {\mdfsetup{% frametitle={% \tikz[baseline=(current bounding box.east),outer sep=0pt] \node[anchor=east,rectangle,fill=blue!20] {\strut Theorem~\thetheo};}} }% {\mdfsetup{% frametitle={% \tikz[baseline=(current bounding box.east),outer sep=0pt] \node[anchor=east,rectangle,fill=blue!20] {\strut Theorem~\thetheo:~#1};}}% }% \mdfsetup{innertopmargin=10pt,linecolor=blue!20,% linewidth=2pt,topline=true, frametitleaboveskip=-12.5pt, } \begin{mdframed}[]\relax% }{\end{mdframed}} – lukas.coenig Mar 12 '16 at 8:05

I recently requested some help with theorem decorations in Theorem decorations that stay with theorem environment. I wanted something the discerns from regular text (to make the theorem stand out), but not too much. Additionally, the environment should be easily breakable across pages without causing problems (with orphaned/widowed decorations) or being visually "broken" (like a truncated frame). The result, after some modifications, yields this:

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{ifmtarg}% http://ctan.org/pkg/ifmtarg
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
\usepackage{environ}% http://ctan.org/pkg/environ
\usepackage{multido}% http://ctan.org/pkg/multido
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

\makeatletter%
\newcommand{\theoremhang}{% top theorem decoration
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,2){\line(1,0){206}}% Top line
\multido{\iA=2+-1,\iB=50+-10}{5}{% Top hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,-1){1}}% Top left hang
\put(203,\iA){\line(0,-1){1}}% Top right hang
}%
\end{picture}%
\endgroup%
}%
\newcommand{\theoremhung}{% bottom theorem decoration
\nobreak
\begingroup%
\setlength{\unitlength}{.005\linewidth}% \linewidth/200
\begin{picture}(0,0)(1.5,0)%
\linethickness{0.45pt} \color{black!50}%
\put(-3,0){\line(1,0){206}}% Bottom line
\multido{\iA=0+1,\iB=50+-10}{5}{% Bottom hangs
\color{black!\iB}%
\put(-3,\iA){\line(0,1){1}}% Bottom left hang
\put(203,\iA){\line(0,1){1}}% Bottom right hang
}%
\end{picture}%
\endgroup%
}%

\newcounter{theorem}
\renewcommand{\thetheorem}{\arabic{theorem}}
\NewEnviron{theorem}[1][]{%
\refstepcounter{theorem}\postdisplaypenalty=10000 %
{\sffamily\bfseries\upshape Theorem \thetheorem\@ifnotmtarg{#1}{\ (#1)}}\ \ \itshape\ignorespaces%
\BODY % Typeset theorem body/content
}
\makeatother

\begin{document}

\lipsum[1]

\begin{theorem}[Special theorem]
\lipsum[2]
\end{theorem}

And then there is also

\begin{theorem}[Short theorem]
This is just a short theorem description.
\end{theorem}

\lipsum[3]

\end{document}


• A small follow-up: How does one prevent page breaks that would make the bottom "frame" appear alone at the beginning of a page? – Andrés E. Caicedo Aug 9 '12 at 18:49

Later in 2011 Thomas F. Sturm published a nice package called tcolorbox for creating such special effects like the ones you desire. Like this you do not need to create such a big overhead in the preamble as the other answers' examples.

Here I give you a MWE but for more details please look at the package manual:

\documentclass{article}
\usepackage[listings]{tcolorbox}
\tcbuselibrary{listings,theorems}

\newtcbtheorem[number within=section]{mytheo}{My Theorem}%
{colback=green!5,colframe=green!35!black,fonttitle=\bfseries}{th}

\begin{document}

\begin{mytheo}{This is the theorem title}{label_of_the_theorem}
This is the text of the theorem. The counter is automatically assigned and,
in this example, prefixed with the section number. This theorem is numbered with
\ref{th:label_of_the_theorem} and is given on page \pageref{th:label_of_the_theorem}.
\end{mytheo}

The theorem enumerator adds the number of the section as indicated in the preamble.

\end{document}