[Tex/LaTex] Difference between \framebox{$$} and \boxed{}

boxesframed

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} 

Best Answer

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}

Result

A simulation of \boxed would be:

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