LATEX Q - boxes are equations

1. Dec 2, 2008

NoobixCube

How do you put a box around a numbered equation?

2. Dec 3, 2008

alphysicist

Hi NoobixCube,

Were you wanting the box around just the equation itself (but not the equation number)? I do that quite often with the boxed command from the amsmath package:

Code (Text):

\documentclass{article}

\usepackage{amsmath}

\begin{document}

some text

\boxed{
x^2+y^2 = z^2
}

more text

\end{document}

If you want the entire line to be boxed (including the equation number), you can nest boxes, something like:

$\fbox{ \addtolength{\linewidth}{-2\fboxsep}% \addtolength{\linewidth}{-2\fboxrule}% \begin{minipage}{\linewidth} x^2+y^2=z^2 \end{minipage} }$

Of course you would not want to type that in for every equation, so you could define a new command once in the preamble, so that your document looks like:

Code (Text):

\documentclass{article}

\newcommand{\boxeqn}[1]{
$\fbox{ \addtolength{\linewidth}{-2\fboxsep}% \addtolength{\linewidth}{-2\fboxrule}% \begin{minipage}{\linewidth} #1 \end{minipage} }$
}

\begin{document}

some text

x^2 + y^2 = z^2

more text

\boxeqn{
a^2 + b^2 = c^2
}

more text

\boxeqn{
\oint \vec B\cdot d\vec A =0
}

\end{document}

In the above definition, the lines with "addtolength" accounts for the separation between the box and its contents, and for the width of the line around the box. (and these lengths are controlled by fboxsep and fboxrule.) With that taken into account, the equation numbers for the boxed and unboxed equations are all lined up. (The comment symbols % keep spaces from being added at the wrong spot.)

EDIT: If you modify the definition (so it is on fewer lines) remember that spaces are needed at the appropriate places. So for example if you tried to combine the third and fourth line like this:

\newcommand{\boxeqn}[1]{
$\fbox{\addtolength{\linewidth}{-2\fboxsep}%%%%<---CHANGING THIS LINE \addtolength{\linewidth}{-2\fboxrule}% \begin{minipage}{\linewidth} #1 \end{minipage} }$
}

so as to put \fbox and the first \addto length on the same line, the equation numbers won't line up any more; you would need to insert a space between those two:

\newcommand{\boxeqn}[1]{
$\fbox{ \addtolength{\linewidth}{-2\fboxsep}% \addtolength{\linewidth}{-2\fboxrule}% \begin{minipage}{\linewidth} #1 \end{minipage} }$
}

Last edited: Dec 3, 2008
3. Dec 3, 2008