LaTeX How to Put a Box Around a Numbered Equation?

  • Thread starter Thread starter NoobixCube
  • Start date Start date
  • Tags Tags
    Latex
AI Thread Summary
To put a box around a numbered equation in LaTeX, the amsmath package's boxed command can be used for just the equation, while nesting boxes allows for boxing the entire line, including the equation number. A new command can be defined in the preamble to streamline this process, ensuring consistent formatting throughout the document. The command includes adjustments for spacing to maintain alignment of equation numbers. Proper handling of spaces is crucial when modifying the command to prevent misalignment. Overall, this method enhances the visual presentation of equations in LaTeX documents.
NoobixCube
Messages
154
Reaction score
0
How do you put a box around a numbered equation?
 
Physics news on Phys.org
Hi NoobixCube,

NoobixCube said:
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:

Code:
\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:

Code:
\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}
}
\]
}
 
Last edited:
That helped loads! Thanks man!
 
Sure, glad to help!
 

Similar threads

Replies
4
Views
2K
Replies
1
Views
1K
Replies
12
Views
3K
Replies
6
Views
5K
Replies
5
Views
3K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
13
Views
3K
Back
Top