5

My question is simple: I use \boxed from the amsmath package and so far so good.

The single issue I have is that I use the boxed environment on its own line, but the box is only around the equation, not the entire line.

Example code:

  \[  \boxed{ p(v,h) = \frac{1}{Z} e ^{-E(v,h)} }  \]

How can I extend the box to the entire line?

Thank you!

2 Answers 2

9

Use the environment mdframed instead of the command \boxed

MWE:

\documentclass{article}
\usepackage{amsmath,mdframed}

\begin{document}

\begin{mdframed}
\[  p(v,h) = \frac{1}{Z} e ^{-E(v,h)}  \]
\end{mdframed}

\end{document}  

Output:

enter image description here

If you need any background you can add a backgroundcolor option to the mdframed environment.

For example:

\documentclass{article}
\usepackage{amsmath,mdframed,xcolor}

\begin{document}

\begin{mdframed}[backgroundcolor=green!10]
\[  p(v,h) = \frac{1}{Z} e ^{-E(v,h)} \]
\end{mdframed}

\begin{mdframed}[backgroundcolor=cyan!20]
\begin{equation}
  p(v,h) = \frac{1}{Z} e ^{-E(v,h)}
\end{equation}
\end{mdframed}

\end{document} 

Output:

enter image description here

1

My solution is a little more unusual, but in the end it lets you keep the equation numbering and lets you highlight the box containing the equation.

To achieve that I defined a \newcommand which I called \fancyboxed which has the following synopsis:

\fancyboxed[<background color>]{<equation>}

Please note that in this case the argument <equation> is already in math mode, so you don't have to insert things such as $...$, \[...\], etc.

The basic idea behind my solution was to treat the equation as the argument of a (rectangular) tikz node spanning the entire \linewidth.

Here is the code:

\documentclass{article}
\usepackage{tikz}          %drawing
\usepackage{amsmath}
\usepackage{lipsum}        %dummy text
\newcommand{\fancyboxed}[2][white]{      %white is default background
  \begin{equation}
    \tikz \node at(0,0) [shape=rectangle,draw,thick,minimum width=\linewidth,fill=#1] {$\displaystyle #2$};
\end{equation}
  }
\begin{document}
\lipsum[1]
\fancyboxed{p(v,h) = \frac{1}{Z} e ^{-E(v,h)}}
\fancyboxed[red!30]{p(v,h) = \frac{1}{Z} e ^{-E(v,h)}}
\lipsum[2]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .