Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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).

share|improve this question
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
    
Welcome to TeX.SE. –  Peter Grill Jan 31 '13 at 3:35
add comment

2 Answers

up vote 12 down vote accepted

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

share|improve this answer
    
Interesting to see how the TeX math routines put the -1 in a visibly too low position. –  jfbu 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. –  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

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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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