53 Comments

  1. Torbjorn

    It would be nice if you changed all the $$ .. $$ to \[ .. \], the former is not recommended to use in LaTeX.

  2. The code above re-numbers the lemma from 1.1. How can I set the counter so that the lemma’s number continues the theorem’s number? (theorem 1.1, then lemma 1.2, then theorem 1.3, and so on). Thanks.

    • tom

      Thanks for your question. I assume you refer to the complete code example at the bottom of the article. You’d just have to replace the lemma counter with the theorem counter. Please see the updated lemma code below.

      Best, Tom

      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %Lemma
      \newenvironment{lem}[2][]{%
      \refstepcounter{theo}%
      \ifstrempty{#1}%
      {\mdfsetup{%
      frametitle={%
      \tikz[baseline=(current bounding box.east),outer sep=0pt]
      \node[anchor=east,rectangle,fill=green!20]
      {\strut Lemma~\thetheo};}}
      }%
      {\mdfsetup{%
      frametitle={%
      \tikz[baseline=(current bounding box.east),outer sep=0pt]
      \node[anchor=east,rectangle,fill=green!20]
      {\strut Lemma~\thetheo:~#1};}}%
      }%
      \mdfsetup{innertopmargin=10pt,linecolor=green!20,%
      linewidth=2pt,topline=true,%
      frametitleaboveskip=\dimexpr-\ht\strutbox\relax
      }
      \begin{mdframed}[]\relax%
      \label{#2}}{\end{mdframed}}
  3. Kim

    If you don’t use the label part in the theorems the first letter in the output will missing.

    \begin{lem}[B\'ezout's identity]
    Let $a$ and $b$ be nonzero integers and let $d$ be their greatest common divisor. Then there exist integers $x$ and $y$ such that:
    \[ax+by=d\]
    \end{lem}
    This is a reference to Bezout's lemma \ref{lem:bezout}

    Output
    et $a$ and $b$ ….. No L!

    Thanks a lot for the examples and good blog by the way.

    -Kim

    • tom

      Hi Kim,

      Thanks for pointing this out. When the label is not used, just leave the curly brackets empty.

      \begin{lem}[B\'ezout's identity]{}

      Alternatively, you can also change the lem environment definition.

      Best, Tom

  4. Jens

    Hi Tom,

    Thank you for this great article! Since I found it early this year, I use these fancy boxes in every script I write (I´m a math student and make summaries).

    Now I like to use such boxes formulas and have problem if my content starts with \[ … \] .
    Then I have a blank line at the beginning.
    If I write a text before \[ … \], then the formula is at the exact same point as without text.

    Can you reproduce this issue? If not, I will make my first minimal example 😉

    Best wishes
    Jens

    • tom

      Hi Jens,

      I can reproduce the extra whitespace with the displaymath environment in the box. There seems to be an extra baselineskip added within mdframed.

      Although it is not an elegant solution, my suggestion would be to manually remove the whitespace where necessary.

      \begin{theo}{thm:theorem1}
      \vspace{-\baselineskip}
      \[a^2+b^2=c^2\]
      \end{theo}

      HTH,
      Tom

      • Jens

        Hi Tom,

        Thanks a lot for this workaround, that helps indeed.

        In case someone finds an elegant solution it would be nice if you could share it here.

        Bye,
        Jens

      • tom

        You are welcome! To stay informed of future replies, you can subscribe to comments on this article.

        Best,
        Tom

    • tom

      Hi there,

      Thanks for your question. You can add extra vertical space before the box through the skipabove option.

      \begin{mdframed}[skipabove=20pt]\relax%
      \label{#2}}{\end{mdframed}}

      HTH,
      Tom

      • tom

        Hi Mads,

        Thanks for letting me know. Perhaps you can try to update your LaTeX distribution to the latest version. I’m happy to try it out, if you send me a minimal working example which produces the boxes in your posted figure.

        HTH, Tom

    • tom

      Hi Sanjana,

      Below’s the example from the article without numbering in the proofs.

      Best wishes,
      Tom

      \documentclass{article}
      \usepackage[framemethod=TikZ]{mdframed}
      \usepackage{amsthm}
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %Proof environment
      \newenvironment{prf}[1][]{%
      \ifstrempty{#1}%
      {\mdfsetup{%
      frametitle={%
      \tikz[baseline=(current bounding box.east),outer sep=0pt]
      \node[anchor=east,rectangle,fill=red!20]
      {\strut Proof};}}
      }%
      {\mdfsetup{%
      frametitle={%
      \tikz[baseline=(current bounding box.east),outer sep=0pt]
      \node[anchor=east,rectangle,fill=red!20]
      {\strut Proof:~#1};}}%
      }%
      \mdfsetup{innertopmargin=10pt,linecolor=red!20,%
      linewidth=2pt,topline=true,%
      frametitleaboveskip=\dimexpr-\ht\strutbox\relax
      }
      \begin{mdframed}[]\relax%
      }{\qed\end{mdframed}}
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %Examples
      \begin{document}
      \section{Test} 
      \begin{prf}
      If $x=y=\sqrt{2}$ is an example, then we are done; otherwise $\sqrt{2}^{\sqrt{2}}$ is irrational, in which case taking $x=\sqrt{2}^{\sqrt{2}}$ and $y=\sqrt{2}$ gives us:
      \[\bigg(\sqrt{2}^{\sqrt{2}}\bigg)^{\sqrt{2}}=\sqrt{2}^{\sqrt{2}\sqrt{2}}=\sqrt{2}^{2}=2.\]
      \end{prf}
      \end{document}
  5. Thank you very much for these fancy boxes! I tried defining a definition (and also theorem, lemma and proof) environment using them. I then wrote two definitions in the same section and both are named ‘Definição 1.0’ (same numbering). Do you have any idea why? Maybe I messed the code up? I couldn’t quite understand the numbering code. Here’s my version of the code:

    \newcounter{deff}[section]\setcounter{deff}{0}
    	\renewcommand{\thedeff}{\arabic{section}.\arabic{deff}}
    	\newenvironment{deff}[2][]{%
    	\refstepcounter{deff}%
    	\ifstrempty{#1}%
    	{\mdfsetup{%
    	frametitle={%
    	\tikz[baseline=(current bounding box.east),outer sep=0pt]
    	\node[anchor=east,rectangle,fill=green!20]
    	{\strut Definição~\thedeff};}}
    	}%
    	{\mdfsetup{%
    	frametitle={%
    	\tikz[baseline=(current bounding box.east),outer sep=0pt]
    	\node[anchor=east,rectangle,fill=green!20]
    	{\strut Definição~\theteo:~#1};}}%
    	}%
    	\mdfsetup{innertopmargin=10pt,linecolor=green!20,%
    	linewidth=2pt,topline=true,%
    	frametitleaboveskip=\dimexpr-\ht\strutbox\relax
    	}
    	\begin{mdframed}[]\relax%
    	\label{#2}}{\end{mdframed}}

    Thanks in advance!

    • Nevermind, found my mistake! The second \theteo was not replaced by \thedeff. Thank you very much again for this tip, I’m using it along with the fncychap package (using Bjornstrup as style) and they look good together. =)

    • tom

      Hi Nataliya,

      Thanks for getting in touch. Did you try to run the complete example at the end of the article? Please provide your code, so I can take a look.

      Best,
      Tom

  6. Kate

    Hi Tom,

    I have been trying to use your code to write up note for my exams. My problem is that the text runs out outwidth the box and equations get messed up. I copied you code letter for letter and I have no idea what has gone wrong. Any help would be appreciated,

    Kind Regards,

    Kate

  7. This is awesome! I have a question: this doesn’t happen often, but some theorem titles are too long to fit into one line. How do I modify the code so that if the title is too long, it carries on to the next line and therefore maybe make the box taller?

    • tom

      Hi Harry,

      Interesting question. Although not very elegant, you could place the title inside a minipage environment. See example below.

      Best, Tom

      \documentclass{article}
      \usepackage[framemethod=TikZ]{mdframed}
      \usepackage{amsthm}
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %Theorem
      \newcounter{theo}[section] \setcounter{theo}{0}
      \renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
      \newenvironment{theo}[2][]{%
      \refstepcounter{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]
      {\begin{minipage}{0.99\linewidth}Theorem~\thetheo:~#1\end{minipage}};}}%
      }%
      \mdfsetup{innertopmargin=10pt,linecolor=blue!20,%
      linewidth=2pt,topline=true,%
      frametitleaboveskip=\dimexpr-\ht\strutbox\relax
      }
      \begin{mdframed}[]\relax%
      \label{#2}}{\end{mdframed}}
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      %Examples
      \begin{document}
      \section{Theorem and lemma examples with title}
      \begin{theo}[Pythagoras' theorem]{thm:pythagoras}
      In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the catheti.
      \[a^2+b^2=c^2\]
      \end{theo}
      \begin{theo}[Pythagoras' theorem with a particularly long heading]{thm:pythagoras}
      In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the catheti.
      \[a^2+b^2=c^2\]
      \end{theo}
      \end{document}
  8. Eron Souza

    Hello Tom

    Thanks for your great work on Latex.

    I’m using your programs in editing a book, however, it appears

    Package kvsetkeys Error: Undefined key `5pt ‘. … [Exponential Fundamental Limit 1] {LFE1}

    even loaded the “kvsetkeys” package.

    Would you help me?

    Thank you very much.

  9. Kaumudi

    Hello Tom,

    I tried inserting a figure into the box, but I get an error: “Float(s) lost”. Is it not possible to insert figures into these boxes?

    Thanks,

    • tom

      Hi Kaumudi,

      I don’t know about floats, but if you just place the figure inside the box it works. See the minimal example below (with the theorem box definition omitted).

      Best, Tom

      \documentclass{article}
      \usepackage{graphicx}
      \usepackage[framemethod=TikZ]{mdframed}
      \usepackage{amsthm}
      
      % Theorem definition goes here. 
      
      \begin{document}
      \section{Image example}
      \begin{theo}[Image]{thm:image}
      \rule{1cm}{1cm}
      %\includegraphics{path/file}
      \end{theo}
      \end{document}
    • tom

      Hi there,

      Citation works exactly the same way as outside a theorem box. Below is a minimal example.

      Best, Tom

      \documentclass{article}
      \usepackage[backend=biber]{biblatex}
      \addbibresource{cite.bib}
      \usepackage[framemethod=TikZ]{mdframed}
      \usepackage{amsthm}
      %%%%%%%
      %Citation example
      \begin{filecontents}{cite.bib}
      @book{mybook,
        author  = {Some, Author},
        title   = {Some title},
        date    = {2017},
        pagetotal   = {150}
      }
      \end{filecontents}
      %%%%%%
      %Theorem
      \newcounter{theo}[section] \setcounter{theo}{0}
      \renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
      \newenvironment{theo}[2][]{%
      \refstepcounter{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=\dimexpr-\ht\strutbox\relax
      }
      \begin{mdframed}[]\relax%
      \label{#2}}{\end{mdframed}}
      %%%%%%
      %Examples
      \begin{document}
      \section{Citation example}
      \begin{theo}[A citation in a theorem]{thm:citation}
      Citation example \cite{mybook}.
      \end{theo}
       \printbibliography 
      \end{document}
  10. anchamath

    Hello Tom

    I have been tried your code. I copied and tried to run it, but I get some error.
    The Error is “Package keyval Error: framemethod undefined. \ProcessKeyvalOptions*”
    Can you help me to fix this problem.
    Thank you

    • Hi there,

      Thanks for your comment. I can’t reproduce the error. The code still works on my system in both, article and beamer document classes. First, I suggest to update to the latest distribution. If that doesn’t help, please provide a minimal working example for me to reproduce the problem.

      Thanks,
      Tom

      • anchamath

        Thank you very much for answering my questions.
        What do you means to update to the latest distribution? Am I need to up date my MikTex?
        Thanks

      • Hi there,

        You said you copied the code from the article above and I assume you referred to the complete example towards the end of the article. As I can run that code on my system, there is a possibility that your distribution is outdated. However, that doesn’t mean it will fix the problem. It’s just all I can say with the limited information I have. Again, it would be very helpful if you provided a minimal example to illustrate the error.

        Best, Tom

  11. DEB BIJAN

    Thanks Tom for these excellent examples.
    On exactly using these codes I could successfully run them, but when there is a break in page, the framed gets broken across places, sometimes only heading in bottom of one page and rest of the body text in next page and looks very bad formatting to read the book.
    How can I stop the frame to get break across pages?
    I want the frame to adjust in the same page or start from a new page if there is break in the frame. Can it be done.Please provide me the code as I am very new learner in LaTeX.

    Is using [nobreak=true] in the mdframe ok??

  12. Atef

    Hallo,
    i use the package “arabtex” and “rydarab” to write araic mathematical document.
    I want to write the theorem title from right to left, but i do not how??
    Can you please help me?

    Best Regards
    Atef Abdel-Rahman

  13. Philipp

    Hi Tom!

    What a wonderful example, I am using it for the book I am writing. 🙂

    I was wondering whether you have an easy solution for allowing line breaks in the theorem title because I have a case where the title is too long and no longer fits the page width…

    Thanks a lot! Philipp

  14. Calvin

    Hello there, thanks for the great work.
    I am having problem with putting several different boxes in my book.
    I need the section number to be increasing, instead they are repeating. How could solve the problem. I need to use about 10 boxes for different purposes: examples. exercises, theorems, try this, problems, axioms, … and the list goes on

    • Hi Calvin,

      The easiest solution would be to define 10 different counters and styles. If you keep the code in a different file, it doesn’t clutter your preamble and you can reuse it if needed.

      The section counter is increased by 1 every time you start a new section with \section{...}. In the example, I reset the counters when the section counter is increased, but you can omit this by not linking them to the section (\newcounter{theo} instead of \newcounter{theo}[section]).

      HTH, Tom

  15. Abdullah Zubair

    Hey Tom, these look really nice. Im compiling a set of course notes for publication, can I consent to use this source code in my latex documentation? (I would like to reference you in the Bibliography)

  16. btut

    Hi! Thanks for the neat boxes, I really like how they can be modified using tikz.

    One thing to note: This does not work well with tikzexternalize. If you are using tikzexternalize, make sure to turn off optimization (\tikzexternalize[optimize=false]). Otherwise, the title will be optimized away. Tikz does not realize that it is nested and thinks it is not used to produce an output.

    Also, I need to manually force tikzexternalize to rebuild the figure after each change. I don’t know why.

Leave a Reply to KaumudiCancel reply