# TikZ -The pretty boxes to frame the theorems-lemma-proposition-etc

I came across a website which provides examples of pretty boxes to frame theorems in LaTeX with extension bioboite and not with TikZ.

Here is code with:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{boiboites}

\newboxedtheorem[boxcolor=orange, background=blue!5, titlebackground=blue!20,
titleboxcolor = black]{theo}{Théorème}{test}

\begin{document}
\begin{theo}[Loi des grands nombres]
Soit $(X_n)_{n\in \mathbb{N}}$ une suite de variables aléatoires réelles
indépendantes identiquement distribuées telles que $X_1 \in L^1$. Alors :
$$\frac{1}{n} \sum_{i=1}^n X_i \overset{\textnormal{p.s.}}{\longrightarrow} \mathbb{E} (X_1) .$$
\end{theo}
\end{document}


Please, if someone could make it in TikZ code with counters independent from theorems, propositions. Also, if possible with different style and color, for example the style of theorems different from propositions or lemmas.

-

Below I show two possibilities; in both cases I defined two structures, one for theorems and the other one for lemmas (with some variations on the colors and in the position of the head); the mechanism should be clear for other structures (the code contains some comments).

1. Using the mdframed package and its framemethod=tikz option (so TikZ is used):

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{calc}
\usepackage{chngcntr}
\usepackage{lipsum}

% counters
\newcounter{theorem}
\newcounter{lemma}
\counterwithin{theorem}{chapter}
\counterwithin{lemma}{chapter}

% names for the structures
\newcommand\theoname{Théorème}
\newcommand\lemmname{Lemme}

\makeatletter
% mdf key for the eventual notes in the structures
\def\mdf@@mynote{}
\define@key{mdf}{mynote}{\def\mdf@@mynote{#1}}

% style for theorems
\mdfdefinestyle{mytheo}{
settings={\refstepcounter{theorem}},
linewidth=1pt,
innertopmargin=1.5\baselineskip,
roundcorner=10pt,
backgroundcolor=blue!05,
linecolor=orange,
singleextra={
\node[xshift=10pt,thick,draw=blue,fill=blue!20,rounded corners,anchor=west] at (P-|O) %
{\strut{\bfseries\theoname~\thetheorem}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}};
},
firstextra={
\node[xshift=10pt,thick,draw=blue,fill=blue!20,rounded corners,anchor=west] at (P-|O) %
{\strut{\bfseries\theoname~\thetheorem}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}};
}
}

% style for lemmas
\mdfdefinestyle{mylemm}{
settings={\refstepcounter{lemma}},
linewidth=1pt,
innertopmargin=1.5\baselineskip,
roundcorner=10pt,
backgroundcolor=red!05,
linecolor=red!70!black,
singleextra={
\path let \p1=(P), \p2=(O) in
node[thick,draw=green!40!black,fill=green!20,rounded corners] at (P-|0.5*\x2+0.5*\x1,0) %
{\strut{\bfseries\lemmname~\thelemma}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}};
},
firstextra={
\path let \p1=(P), \p2=(O) in
node[thick,draw=green!40!black,fill=green!20,rounded corners] at (P-|0.5*\x2+0.5*\x1,0) %
{\strut{\bfseries\lemmname~\thelemma}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}};
}
}

% some auxiliary environments
\newmdenv[style=mytheo]{theor}
\newmdenv[style=mylemm]{lemm}

% the actual environments
\newenvironment{theorem}[1][]
{\begin{theor}[mynote=#1]}
{\end{theor}}
\newenvironment{lemma}[1][]
{\begin{lemm}[mynote=#1]}
{\end{lemm}}

\makeatother

\begin{document}

\chapter{Test chapter}
\begin{theorem}
\lipsum[4]
\end{theorem}
\begin{lemma}[Lemme de Zorn]
\lipsum[4]
\end{lemma}
\begin{theorem}[Loi des grands nombres]
\lipsum[4]
\end{theorem}

\end{document}


1. Using tcolorbox :

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\usepackage{chngcntr}
\usepackage{lipsum}

% counters
\newcounter{theorem}
\newcounter{lemma}
\counterwithin{theorem}{chapter}
\counterwithin{lemma}{chapter}

% names for the structures
\newcommand\theoname{Théorème}
\newcommand\lemmname{Lemme}

\makeatletter

% environment for theorems
\newtcolorbox{theorem}[1][]{
breakable,
enhanced,
colback=blue!05,
colframe=orange,
top=\baselineskip,
enlarge top by=\topsep,
overlay unbroken and first={
\node[xshift=10pt,thick,draw=blue,fill=blue!20,rounded corners,anchor=west] at (frame.north west) %
{\refstepcounter{theorem}\strut{\bfseries\theoname~\thetheorem}\if#1\@empty\relax\relax\else~(#1)\fi};
}
}

% environment for lemas
\newtcolorbox{lemma}[1][]{
breakable,
enhanced,
colback=red!05,
colframe=red!70!black,
top=\baselineskip,
enlarge top by=\topsep,
overlay unbroken and first={
\node[thick,draw=green!40!black,fill=green!20,rounded corners] at (frame.north) %
{\refstepcounter{lemma}\strut{\bfseries\lemmname~\thelemma}\if#1\@empty\relax\relax\else~(#1)\fi};
}
}

\makeatother

\begin{document}

\chapter{Test chapter}
\begin{theorem}
\lipsum[4]
\end{theorem}
\begin{lemma}[Lemme de Zorn]
\lipsum[4]
\end{lemma}
\begin{theorem}[Loi des grands nombres]
\lipsum[4]
\end{theorem}

\end{document}


Personally I think that using independent counters is not the best choice (for the reader, at least); I would use the same counter for the structures.

-
Nice. Try to do that with MS word :) –  Nasser Jul 7 '13 at 2:44
Hi, I would like to thank all and specially to express my heartfelt thanks to Mr Gonzalo Medina for his inspiration, and his great efforts to explain things clearlya nd make it simply to use . –  Educ Jul 7 '13 at 6:04
@Educ Thank you for your kind words. I am glad I could help. –  Gonzalo Medina Jul 7 '13 at 15:26
@Educ I updated my answer with another option, using this time tcolorbox. –  Gonzalo Medina Jul 7 '13 at 17:37
Thanks again Mr Gonzalo Medina –  Educ Jul 7 '13 at 21:06

Maybe you should have taken the time to read the help provided on the webpage were you found the style file ;)

http://alexisfles.ch/en/latex/boiboites.html

http://snouffy.free.fr/blog-en/index.php/post/2010/01/30/Nice-boxes-for-your-theorems-with-tikz

if you want, say, a definition environment and a theorem one, with different counters and colors, you can simply write:

\newboxedtheorem[thcounter=section,boxcolor=orange, background=blue!5, titlebackground=blue!20,titleboxcolor = black, thcounter=section]{theorem}{Theorem}{somecounter}
\newboxedtheorem[thcounter=section,boxcolor=blue, background=green!5, titlebackground=purple!20,titleboxcolor = yellow, hcounter=section]{definition}{Definition}{anothercounter}


Granted, you can't tweak everything : you can't, for example, put the title in the middle of the box as Gonzalo Medina did in his answer, but you can still do what you asked for !