View Full Version : LATEX Q - boxes are equations
NoobixCube
Dec2-08, 10:43 PM
How do you put a box around a numbered equation?
alphysicist
Dec3-08, 08:05 AM
Hi NoobixCube,
How do you put a box around a numbered equation?
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:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
some text
\begin{equation}
\boxed{
x^2+y^2 = z^2
}
\end{equation}
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}
\begin{equation}
x^2+y^2=z^2
\end{equation}
\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:
\documentclass{article}
\newcommand{\boxeqn}[1]{
\[
\fbox{
\addtolength{\linewidth}{-2\fboxsep}%
\addtolength{\linewidth}{-2\fboxrule}%
\begin{minipage}{\linewidth}
\begin{equation}
#1
\end{equation}
\end{minipage}
}
\]
}
\begin{document}
some text
\begin{equation}
x^2 + y^2 = z^2
\end{equation}
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}
\begin{equation}
#1
\end{equation}
\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}
\begin{equation}
#1
\end{equation}
\end{minipage}
}
\]
}
NoobixCube
Dec3-08, 07:38 PM
That helped loads! Thanks man!
alphysicist
Dec3-08, 09:49 PM
Sure, glad to help!
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.