# TeX equivalent for LaTeX's \boxed

Does anyone have any suggestions for recreating the \boxed{...} function from LaTeX in plain TeX? I'm pretty sure it's part of the amsmath package for LaTeX (not actually sure because I include amsmath it in all of my LaTeX files).

-
From amsmath.dtx: \newcommand{\boxed}[1]{\fbox{\m@th$\displaystyle#1$}}; and you can extract the definition of \fbox (and friends) from latex.ltx. –  Werner Jan 31 '13 at 3:22
–  Peter Grill Jan 31 '13 at 3:35
add comment

## 2 Answers

You can recycle the definition of \cstok that you can find in manmac.tex:

\def\boxed#1{% from \cstok in manmac.tex
\leavevmode\thinspace
\hbox{\vrule\vtop{\vbox{\hrule\kern1pt
\hbox{\thinspace$\displaystyle{#1}$\thinspace}}
\kern1pt\hrule}\vrule}\thinspace}

$$a = \boxed{1\over2} = {2\over 4} = \boxed{\root3\of{8^{-1}}}$$

\bye

Adjust the spacing to suit your taste.

-
Interesting to see how the TeX math routines put the -1 in a visibly too low position. –  jfbu Jan 31 '13 at 9:01
@jfbu The argument to \radical is typeset in cramped text style (in this case). You can force "non cramped" with an explicit \textstyle or \displaystyle declaration. –  egreg Jan 31 '13 at 9:02
surprising though that with an overall displaystyle, TeX decides that stuff within a \root3\of should be in cramped style, as if this was destined for an inline equation. –  jfbu Jan 31 '13 at 9:06
another interesting aspect is that removing the braces around the #1 would be a very bad idea, as the \displaystyle scope is ended by the \over in \boxed{1\over2} for example. –  jfbu Jan 31 '13 at 9:10
@jfbu In my first attempt I missed those braces. ;-) But I immediately realized the mistake because I used 1\over2. –  egreg Jan 31 '13 at 9:11
show 1 more comment

I have done Werner's exercise. With the additional braces for use with \over as in egreg's answer.

Edit for my first ever non zero use of \mathsurround. But sub-formulas (in \hboxes) then should be aware and set their own, as is systematic I think in LaTeX/amsmath.

\newdimen\fboxsep  \fboxsep3pt
\newdimen\fboxrule \fboxrule.4pt
\newbox\boxedbox

\long\def\boxed #1{\leavevmode
\setbox \boxedbox =
%%\hbox{\kern\fboxsep {\mathsurround0pt $\displaystyle {#1}$}\kern\fboxsep}%
\hbox{{\mathsurround\fboxsep $\displaystyle {#1}$}}%
\dimen0=\fboxrule\advance\dimen0\fboxsep\advance\dimen0\dp\boxedbox
\hbox{\lower \dimen0
\hbox {%
\vbox {\hrule height \fboxrule
\hbox {\vrule width \fboxrule
\vbox {\vskip \fboxsep \box \boxedbox \vskip \fboxsep }%
\vrule width \fboxrule }%
\hrule height \fboxrule }}}}

$$a = \boxed{1\over2} = {2\over 4} = \boxed{\root3\of{8^{-1}}}$$

\bye

The second being the output of

\documentclass{article}
\usepackage{amsmath}

\begin{document}\thispagestyle{empty}

$$a = \boxed{{1\over2}} = {2\over 4} = \boxed{\root3\of{8^{-1}}}$$

\end{document}
-
in passing I removed the \color@begingroup and \color@endgroup, not having checked if package color could be used with plain TeX. –  jfbu Jan 31 '13 at 15:11
actually, \hbox{{\mathsurround\fboxsep $\displaystyle {#1}$}} would give the first ever actual use of mathsurround I would have ever done. In LaTeX2e they use \m@th for \mathsurround0pt but here \mathsurround\fboxsep would spare some tokens... –  jfbu Jan 31 '13 at 15:38
add comment