Box around an equation in LaTeX (boxed)
\boxed{...} from amsmath draws a frame around a formula or a part of it. \Aboxed from mathtools does the same for one line of align.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{mathtools}
\usepackage{xcolor}Frame a formula
\boxed works in every math environment and sets its content in display size. In equation the number stays outside the frame.
\[
\boxed{E = mc^2}
\]
\begin{equation}
\boxed{a^2 + b^2 = c^2}
\end{equation}Padding and line width
\fboxsep is the distance between content and frame, 3 pt by default. \fboxrule is the width of the line, 0.4 pt by default. The outer braces limit the change to this formula.
{\setlength{\fboxsep}{8pt}\setlength{\fboxrule}{1pt}
\[
\boxed{x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}}
\]
}Frame one line of align
\boxed cannot contain the & of align. \Aboxed{left &= right} from mathtools frames the whole line and keeps the alignment.
\begin{align}
f(x) &= (x+1)^2 \\
\Aboxed{f(x) &= x^2 + 2x + 1}
\end{align}Coloured frame and background
\fcolorbox{frame}{background}{content} and \colorbox{background}{content} come from xcolor. Their content is text, so the formula needs $...$, and \displaystyle for the large form.
\[
\fcolorbox{blue}{yellow!20}{$\displaystyle \int_0^1 x \, dx = \frac{1}{2}$}
\qquad
\colorbox{yellow!40}{$a^2 + b^2 = c^2$}
\]Frame several lines together
The empheq package wraps an amsmath environment, named in the second argument. box=\fbox draws one frame around all lines; the equation numbers stay outside.
Complete document:
\documentclass{article}
\usepackage{empheq}
\begin{document}
\begin{empheq}[box=\fbox]{align}
a &= b + c \\
d &= e + f
\end{empheq}
\end{document}Pitfalls
\boxedwithoutamsmathstops with Undefined control sequence.\fbox{$...$}works without a package but sets the formula in text size.- For rounded corners or a shaded background,
tcolorboxwith\tcbuselibrary{theorems}has\tcboxmath{...}.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
amsmath - Package
mathtools - Package
xcolor - Package
tcolorbox - How-to Draw a box around text in LaTeX