# Difference between \framebox{} and \boxed{}

I want to put an equation in a frame box. Is it the same that if I create a framed text box environment first then put the equation inside or use the environment \boxed{}?

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\begin{document}

\framebox{$a^2+b^2=c^2$}

$\boxed{a^2+b^2=c^2}$

\end{document}


Package amsmath defines \boxed:

\newcommand{\boxed}[1]{\fbox{\m@th$\displaystyle#1$}}


\framebox and \fbox are just different interfaces for the same internal \@frameb@x, which actually makes the box. \framebox has more options.

Thus, the main difference between \framebox{$...$} and $\boxed{...}$ is that \boxed sets \displaystyle, whereas it had to be done manually in the former variant: \framebox{$\displaystyle ...$}.

Another difference appears, if \mathsurround is not zero. This space is set, when TeX enters and leaves inline math mode. It is intended as separation of math from the surrounding text. Inside the box it does not make sense and \boxed removes it by setting \mathsurround to zero by \m@th.

A test file, which illustrates the differences:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}

\begin{document}
\newcommand*{\vs}{\mathrel{\text{vs.}}}
\begin{align*}
\text{\ttfamily\string\framebox\{\$\dots\$\}} &\vs
\text{\ttfamily\string\boxed\{\dots\}}
\\
\framebox{$\frac{123}{456}$} &\vs \boxed{\frac{123}{456}}
\\
\setlength{\mathsurround}{1em}\framebox{$X$} &\vs
\setlength{\mathsurround}{1em}\boxed{X}
\end{align*}
\end{document}


A simulation of \boxed would be:

\framebox{%
\setlength{\mathsurround}{0pt}%
$\displaystyle ...$%
}

• Thanks for the helpful reply! By the way, would you please tell me where I can find the definition of \boxed? – Zhang Ze Jul 2 '15 at 10:43
• @ZhangZe kpsewhich amsmath.sty (or locate the package file amsmath.sty by means of the OS), then line 307 (in version 2013/01/14 v2.14). – Heiko Oberdiek Jul 2 '15 at 11:06
• Okay. Gotta it! – Zhang Ze Jul 2 '15 at 11:17

For such questions I just load the package lua-visual-debug. You can see clearly that they are both the same:

% arara: lualatex

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{blindtext}
\usepackage{lua-visual-debug}
\parindent0pt

\begin{document}
\blindtext

\framebox{$\displaystyle a^2+b^2=c^2$} % displaystyle needed here, as it is set in \boxed by default. It just changes the horizontal spacing of the powers a bit.

$\boxed{a^2+b^2=c^2}$

\blindtext
\end{document}


The boxing is equal speaking of spacing and aligning. Therefore, both approaches are similar. For the syntactical equivalence, see Heiko's answer.