135

I often used \boxed{...} from the AMS math package to place a box around important equations. However, this approach often produces somewhat awkward looking output. Consider

\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}
\begin{document}
 \[
 \boxed{c_i = \sum_jA_{ij}}
 \]
 \[
 \boxed{c_i = \langle\psi|\phi\rangle}
 \]
\end{document}

enter image description here

Observe that in the first case the box looks unbalanced while in the second case could do with more padding on both the top and bottom. What is the best way to go about this? I know that I could use a \phantom but am wondering if more elegant solutions exist.

  • 17
    @David Hammen: I have seen world class PhD theses with boxed equations to emphasise an important equation. – Danie Els Jun 13 '11 at 05:37
  • 12
    @Danie: I'm curious. What makes a Ph.D. thesis world class? – TH. Jun 13 '11 at 06:27
  • 2
    @TH: Contents, contents and contents. Those theses (together with subsequent publications) that attract a lot of citations. – Danie Els Jun 13 '11 at 06:57
  • 1
    Danie: I'm not really sure how that's related. Most papers I read are extremely poorly typeset. Most of my coauthors don't care at all about how papers look beyond a bare minimum. And Ph.D. theses are notoriously ugly. – TH. Jun 13 '11 at 07:08
  • 21
    @TH: David Hammen has commented that emphasised (boxed) equitions are only used for elementry level books. My answer was that I've seen it being used with good effect up to the highest levels of academic publishing. The issues of what is a world class thesis and bad typesetting has nothing to do with boxed equations. So what is you problem here? – Danie Els Jun 13 '11 at 08:02
  • @Danie: I agree with David and I don't think that a thesis counts as academic publishing (especially in that it isn't published). A journal article or (nonelementary) book that uses boxed equations would be far better evidence that it is used outside of elementary level texts. That's all. Obviously, I don't have anything invested in this. I've just never seen it done in a publication. – TH. Jun 13 '11 at 08:24
  • 4
    DavidHammen Wrong; a load of physics and mathematics textbooks use boxes (regardless of the level).

    @TH- Who says it's for publication?

    – User 17670 Apr 21 '13 at 14:42
  • 6
    @David Hammen: The book Classical Electromagnetic Radiation, Third Edition, by Heald and Marion (which is definitely beyond high school), make heavy use of boxed equation — a habit I realized that I could probably beneficially adopt as it highlights which equations are actually important results and which you can safely skim off. As long as it makes the text easier to read, who cares about conventions and what is considered normal? – StrawberryFieldsForever Mar 15 '14 at 18:30
  • 9
    Boxed equations also appear in Spivak's geometry books, though sparingly. Spivak's use is good, in my opinion: his books are quite expository, featuring lots of text and explanation; boxing some key equations allows his text to also be used as a reference. – James Fennell Jun 05 '14 at 16:22
  • I don't like boxing equations, but if you do it, please be sparing and make the box very light, perhaps a light gray. – Benjamin McKay Jan 19 '16 at 10:53

4 Answers4

66

You can use the empheq package and then define your own boxing command. It can be a standard Latex \fbox or a Tikz box, or any other type of box. Look at the example below. I have defined a color box (to make it more interesting) with two optional arguments for padding the space above and below the equation

\mybluebox[<top pad>][<bot pad>]{<contents>}

The keyval package is already loaded so you can make a fancy keyval interface, but I leave that as an exercise to the reader ;-)

\documentclass{article}

\usepackage{color}
\definecolor{myblue}{rgb}{.8, .8, 1}

\usepackage{amsmath}
\usepackage{empheq}

\newlength\mytemplen
\newsavebox\mytempbox

\makeatletter
\newcommand\mybluebox{%
    \@ifnextchar[%]
       {\@mybluebox}%
       {\@mybluebox[0pt]}}

\def\@mybluebox[#1]{%
    \@ifnextchar[%]
       {\@@mybluebox[#1]}%
       {\@@mybluebox[#1][0pt]}}

\def\@@mybluebox[#1][#2]#3{
    \sbox\mytempbox{#3}%
    \mytemplen\ht\mytempbox
    \advance\mytemplen #1\relax
    \ht\mytempbox\mytemplen
    \mytemplen\dp\mytempbox
    \advance\mytemplen #2\relax
    \dp\mytempbox\mytemplen
    \colorbox{myblue}{\hspace{1em}\usebox{\mytempbox}\hspace{1em}}}

\makeatother

\begin{document}
\begin{empheq}[box={\mybluebox[5pt]}]{equation*}
    c_i = \sum_j A_{ij}
\end{empheq}

\begin{empheq}[box={\mybluebox[2pt][2pt]}]{equation*}
    c_i = \langle\psi|\phi\rangle
\end{empheq}
\end{document}

enter image description here

Danie Els
  • 19,694
  • 2
    Will any of these work within an align environment where I only want to shade one of the equations. – Peter Grill Jun 14 '11 at 03:40
  • 1
    @Peter Grill: The \Aboxed command form the mathtools package can box one line of and aligned environment. To change it to shadeding the line, the internal command \@Aboxed need to be redefined, but it is not to difficult. – Danie Els Jun 14 '11 at 05:33
55

tcolorbox also offers several options for boxed math expressions.

tcbhighmath is an special box which can be used with empheq package, but it's also easy to declare new boxes to use with empheq.

Some examples with the code:

enter image description here

\documentclass{article}

\usepackage{amsmath}
\usepackage{empheq}
\usepackage[most]{tcolorbox}

\newtcbox{\mymath}[1][]{%
    nobeforeafter, math upper, tcbox raise base,
    enhanced, colframe=blue!30!black,
    colback=blue!30, boxrule=1pt,
    #1}

\begin{document}
\begin{empheq}[box=\tcbhighmath]{equation*}
    c_i = \langle\psi|\phi\rangle
\end{empheq}

\tcbset{highlight math style={boxsep=5mm,colback=blue!30!red!30!white}}

\begin{empheq}[box=\tcbhighmath]{equation*}
    c_i = \langle\psi|\phi\rangle
\end{empheq}

\begin{empheq}[box=\mymath]{equation*}
    c_i = \langle\psi|\phi\rangle
\end{empheq}

\begin{empheq}[box={\mymath[colback=red!30,drop lifted shadow, sharp corners]}]{equation*}
    c_i = \langle\psi|\phi\rangle
\end{empheq}

\end{document}
Ignasi
  • 136,588
  • 7
    the last boxed equation looks super cool – crypto Aug 15 '16 at 08:54
  • @Ignasi How to add vertical margin below the box? (Space between the box and the following paragraph, etc.) Tried after skip but that did not work. – blackened Apr 11 '17 at 11:28
  • 1
    @blackened: This works for me: box={\mymath[after=\vspace{1cm}]} – Ignasi Apr 11 '17 at 12:53
  • @Ignasi I have added before=\vspace{1cm}, but it did not add vertical space above the box? Is that expected? – blackened Apr 17 '17 at 17:50
  • @blackened I don't know if it's the expected result and as I also don't know what to answer, I think it's better that you open a new question and hope that someone else can answer it. – Ignasi Apr 18 '17 at 18:12
  • @Ignasi If my equation is large and is occupying two lines how do I trigger next line? – Mathematics Aug 22 '20 at 08:07
12

Perhaps these commands will be of some help:

\newcommand{\boxedeq}[2]{\begin{empheq}[box={\fboxsep=6pt\fbox}]{align}\label{#1}#2\end{empheq}}
\newcommand{\coloredeq}[2]{\begin{empheq}[box=\colorbox{lightgreen}]{align}\label{#1}#2\end{empheq}}

Don't forget to put

\usepackage{empheq} 
\usepackage{xcolor}
\definecolor{lightgreen}{HTML}{90EE90}

in the preamble.

Full example:

\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}

\usepackage{empheq}
\usepackage{xcolor}
\definecolor{lightgreen}{HTML}{90EE90}
\newcommand{\boxedeq}[2]{\begin{empheq}[box={\fboxsep=6pt\fbox}]{align}\label{#1}#2\end{empheq}}
\newcommand{\coloredeq}[2]{\begin{empheq}[box=\colorbox{lightgreen}]{align}\label{#1}#2\end{empheq}}

\begin{document}
 \boxedeq{eq:first}{c_i = \sum_jA_{ij}}
 \coloredeq{eq:second}{c_i = \langle\psi|\phi\rangle}
\end{document}

Result

Heiko Oberdiek
  • 271,626
user1999
  • 1,464
2

For future reference, I found this presentation with a few options. I settled on bclogo package. Its documentation is written in French, but it looks like the more versatile option, having icons, framing, background color, title, among others.

  • 12
    Suppose the link is broken then your answer is useless. Please can you add in own words a summary that answers the question? (not all people can read or understand French) – Mensch Jan 30 '16 at 12:59