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!5 | Thickness and colour of the frame, and the background colour. |
topline=false, bottomline=false, rightline=false | Switches 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}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
roundcornerhas no effect with the default frame method. Load the package withframemethod=tikz.- Colour expressions such as
red!5needxcolor. Without it, the run stops with an undefined colour. - A float such as
figureinside the frame gives Not in outer par mode.
Related pages
- How-to Draw a box around text in LaTeX
- Package
tcolorbox - Package
xcolor - Error Not in outer par mode