The mdframed package

Draws a frame with an optional background around text that may run over several pages: the mdframed environment and \newmdenv.

Loading the package

\usepackage{mdframed}

\fbox and \parbox cannot break across pages. An mdframed environment can: the frame and its background continue on the next page. Line width, line colour, background colour and the inner and outer margins are options.

Each of the four lines can be switched off separately, which gives a vertical bar at the left of a paragraph. \newmdenv defines an environment with fixed options, and \surroundwithmdframed puts a frame around an existing environment such as a theorem.

The last release of the package is from 2013. tcolorbox covers the same tasks and is still developed.

Main commands

\begin{mdframed} ... \end{mdframed}Frame around the content; breaks across pages.
linewidth=1pt, linecolor=red, backgroundcolor=red!5Thickness and colour of the frame, and the background colour.
topline=false, bottomline=false, rightline=falseSwitches single lines off; here only the left line remains.
frametitle={Warning}Bold title at the top of the frame.
\newmdenv[linecolor=blue]{sidebar}Defines the environment sidebar with these options.
\surroundwithmdframed[linewidth=1pt]{theorem}Frames every theorem environment.
\usepackage[framemethod=tikz]{mdframed}Draws the frame with TikZ, which the option roundcorner needs.

Complete example

\documentclass{article}
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed}
\newmdenv[linecolor=blue, linewidth=2pt, backgroundcolor=blue!5,
  topline=false, bottomline=false, rightline=false]{sidebar}
\begin{document}
\begin{mdframed}
A frame with the default settings.
\end{mdframed}

\begin{mdframed}[linecolor=red, backgroundcolor=red!5,
  roundcorner=6pt, frametitle={Warning}]
Rounded corners need the TikZ frame method.
\end{mdframed}

\begin{sidebar}
An environment of your own with a line on the left only.
\end{sidebar}
\end{document}
Result
The compiled example

Open in the editor

Compiled with pdfLaTeX from TeX Live 2023.

Pitfalls

Related pages