LaTeX How to Put a Box Around a Numbered Equation?

  • Thread starter Thread starter NoobixCube
  • Start date Start date
  • Tags Tags
    Latex
Click For Summary
SUMMARY

This discussion provides a comprehensive guide on how to box a numbered equation in LaTeX using the amsmath package. The user can utilize the \texttt{\textbackslash boxed} command for individual equations or nest boxes for the entire line, including the equation number. A custom command \texttt{\textbackslash boxeqn} is defined to streamline the process, ensuring proper alignment of equation numbers. The discussion emphasizes the importance of spacing when modifying the command to maintain formatting integrity.

PREREQUISITES
  • Familiarity with LaTeX document structure
  • Understanding of the amsmath package
  • Basic knowledge of LaTeX commands and environments
  • Experience with custom command definitions in LaTeX
NEXT STEPS
  • Explore advanced features of the amsmath package
  • Learn about LaTeX document classes and their configurations
  • Research best practices for defining custom commands in LaTeX
  • Investigate formatting techniques for mathematical documents in LaTeX
USEFUL FOR

LaTeX users, mathematicians, educators, and anyone involved in typesetting mathematical documents who seeks to enhance the presentation of equations.

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