22

This question is two-fold. In my document, I currently have boxes around single equations like so:

\documentclass[11pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\setlength\fboxsep{0.25cm}
\setlength\fboxrule{0.4pt}
\boxed{\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}=0. 
\end{equation}
\end{document}
  • I would now like to be able to adjust the fboxsep for each side top and bottom margins separately from the left and right margins

  • Also, I would like to box - in the same style - multiple equations that are aligned.

How can I do these?

(a) shows the output of corresponding to the box in the MWE, (b) shows an example of what I'd like to get from adjusting the left and right margins independently of the top and bottom ones, (c) shows what how I'd like - in a consistent manner - the boxing of equations that are currently in an align environment (inside a subequations):

enter image description here

29

The empheq package is your friend. The wide box is made with a normal box, with some space left and right, that you can adjust to your likings.

\documentclass[11pt]{article}
\usepackage{empheq}

\newcommand*\widefbox[1]{\fbox{\hspace{2em}#1\hspace{2em}}}

\begin{document}
\begin{empheq}[box=\widefbox]{equation}
  \bar{\nabla}^{\mu} \bar{h}_{\mu\nu} = 0
\end{empheq}

\begin{subequations}
\begin{empheq}[box=\widefbox]{align}
  \bar{\nabla}^{\mu} \bar{h}_{\mu\nu} & = 0 \\
  \bar{\nabla}^{\mu} \bar{h}_{\mu\nu} & = 0
\end{empheq}
\end{subequations}
\end{document}

yields

sample output

  • 1
    Thanks, how would I go about adjusting the vertical margins? – User 17670 Apr 21 '13 at 16:30
  • 2
    @User17670 Use \setlength\fboxsep{0.5cm} which will increase the space around the formula in all directions. Then you can adapt \widefbox again. – mafp Apr 21 '13 at 16:33
8

tcolorbox package provides commands to produce colorful framed boxes which can also be applied to math environments.

\tcboxmath and \tcbhighmath commands add boxes to math expressions. The second one is used to highlight some part of a math expression, even inside a boxed one. Default configurations for these kind of boxes look like

\begin{equation}
\tcboxmath{\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}=0.}
\end{equation}

\begin{equation}
\tcboxmath{\tcbhighmath{\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}}=0.}
\end{equation}

enter image description here

If equation number must be included into the box, theorems library provides ams equation, ams align and ams gather environments. Two customized examples:

\newtcolorbox{mymathbox}[1][]{colback=white, sharp corners, #1}

\begin{mymathbox}[ams equation]
\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}=0.
\end{mymathbox}

\begin{mymathbox}[ams gather, title=two equations, colframe=blue!30!black]
\sum\limits_{n=1}^{\infty} \frac{1}{n} = \infty.\\
\int x^2 ~\text{d}x = \frac13 x^3 + c.
\end{mymathbox}

enter image description here

Finally, tcboxes can be used to define boxes compatible with empheq package to box multiline equations (no number inside box).

\newtcbox{\othermathbox}[1][]{nobeforeafter, math upper, tcbox raise base, 
          enhanced, sharp corners, colback=black!10, colframe=red!30!black, 
          drop fuzzy shadow, left=1em, top=2em, right=3em, bottom=4em}

left, top, right and bottom values define extra margins inside the box.

\begin{empheq}[box=\othermathbox]{align}
\sum\limits_{n=1}^{\infty} \frac{1}{n} &= \infty.\\
\int x^2 ~\text{d}x &= \frac13 x^3 + c.
\end{empheq} 

enter image description here

The complete code

\documentclass{report}

\usepackage{amsmath}
\usepackage{empheq}
\usepackage[theorems,skins]{tcolorbox}

\newtcolorbox{mymathbox}[1][]{colback=white, sharp corners, #1}

\newtcbox{\othermathbox}[1][]{nobeforeafter, math upper, tcbox raise base, enhanced, sharp corners, colback=black!10, colframe=red!30!black, drop fuzzy shadow, left=1em, top=2em, right=3em, bottom=4em}

\begin{document}

\begin{equation}
\tcboxmath{\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}=0.}
\end{equation}

\begin{equation}
\tcboxmath{\tcbhighmath{\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}}=0.}
\end{equation}

\begin{mymathbox}[ams equation]
\bar{\nabla}^{\mu}\bar{h}_{\mu\nu}=0.
\end{mymathbox}

\begin{mymathbox}[ams gather, title=two equations, colframe=blue!30!black]
\sum\limits_{n=1}^{\infty} \frac{1}{n} = \infty.\\
\int x^2 ~\text{d}x = \frac13 x^3 + c.
\end{mymathbox}

\begin{empheq}[box=\othermathbox]{align}
\sum\limits_{n=1}^{\infty} \frac{1}{n} &= \infty.\\
\int x^2 ~\text{d}x &= \frac13 x^3 + c.
\end{empheq}

\end{document}
1

For part A, \fbox uses \kern\fboxsep to make horizontal whitespace around the boxed thing, and then \@frameb@x uses \vskip\fboxsep to wrap it in vertical whitespace. So one quick-and dirty-solution would be to introduce two separate lengths, \fboxseph and \fboxsepv, and reimplement the commands word-for-word, only using different lengths for vertical and horisontal space.

Use \longboxed instead of \boxed after you include the following in the preamble:

\newlength\fboxseph
\newlength\fboxsepv

\setlength\fboxsepv{0.25cm}
\setlength\fboxseph{1cm}

\makeatletter

\def\longboxed#1{\leavevmode\setbox\@tempboxa\hbox{\color@begingroup%
\kern\fboxseph{\m@th$\displaystyle #1 $}\kern\fboxseph%
\color@endgroup }\my@frameb@x\relax}

\def\my@frameb@x#1{%
\@tempdima\fboxrule \advance\@tempdima \fboxsepv \advance\@tempdima \dp\@tempboxa\hbox {%
\lower \@tempdima \hbox {%
\vbox {\hrule\@height\fboxrule \hbox{\vrule\@width\fboxrule #1 \vbox{%
\vskip\fboxsepv \box\@tempboxa \vskip\fboxsepv}#1 \vrule\@width\fboxrule }%
\hrule \@height \fboxrule }}}}

\makeatother

Have no idea about Part B, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.