tikz-pgf,boxes,tikz-styles – Creating Pretty Boxes for Theorems and Lemmas with TikZ

boxestikz-pgftikz-styles

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

enter image description here

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.

Best Answer

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}
    

enter image description here

  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}
    

enter image description here

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.

Related Question