Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

LATEX Q - boxes are equations

  1. Dec 2, 2008 #1
    How do you put a box around a numbered equation?
     
  2. jcsd
  3. Dec 3, 2008 #2

    alphysicist

    User Avatar
    Homework Helper

    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

    \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 (Text):

    \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: Dec 3, 2008
  4. Dec 3, 2008 #3
    That helped loads! Thanks man!
     
  5. Dec 3, 2008 #4

    alphysicist

    User Avatar
    Homework Helper

    Sure, glad to help!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: LATEX Q - boxes are equations
  1. Latex equations Help (Replies: 2)

Loading...