How to Put a Box Around a Numbered Equation?

  • Context: LaTeX 
  • Thread starter Thread starter NoobixCube
  • Start date Start date
  • Tags Tags
    Latex
Click For Summary

Discussion Overview

The discussion centers around how to put a box around a numbered equation in LaTeX, exploring different methods and commands available in the amsmath package. The scope includes technical explanations and practical applications for formatting equations in documents.

Discussion Character

  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant asks how to box a numbered equation.
  • Another participant suggests using the \boxed command from the amsmath package for boxing just the equation, while providing a LaTeX example.
  • A method to box the entire line, including the equation number, is proposed, involving nested boxes and adjustments to line width.
  • A new command, \boxeqn, is defined to simplify the boxing of equations, with details on how to maintain alignment of equation numbers.
  • Participants discuss the importance of spacing in the LaTeX commands to ensure proper formatting.

Areas of Agreement / Disagreement

Participants generally agree on the methods presented for boxing equations, but there is no explicit consensus on a single best approach, as different methods are provided for different preferences.

Contextual Notes

Limitations include the need for careful attention to spacing in LaTeX commands and the potential for misalignment if commands are modified without proper adjustments.

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 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K