11

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.

  • 4
    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
13

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.

enter image description here

  • Interesting to see how the TeX math routines put the -1 in a visibly too low position. – user4686 Jan 31 '13 at 9:01
  • 2
    @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. – user4686 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. – user4686 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
6

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

boxed formulas boxedams

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. – user4686 Jan 31 '13 at 15:11
  • 1
    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... – user4686 Jan 31 '13 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.