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.

10
  • 17
    @David Hammen: I have seen world class PhD theses with boxed equations to emphasise an important equation.
    – Danie Els
    Jun 13, 2011 at 5:37
  • 12
    @Danie: I'm curious. What makes a Ph.D. thesis world class?
    – TH.
    Jun 13, 2011 at 6:27
  • 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, 2011 at 8:02
  • 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? Mar 15, 2014 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. Jun 5, 2014 at 16:22

4 Answers 4

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

2
  • 2
    Will any of these work within an align environment where I only want to shade one of the equations. Jun 14, 2011 at 3: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, 2011 at 5: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}
6
  • 7
    the last boxed equation looks super cool
    – crypto
    Aug 15, 2016 at 8: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, 2017 at 11:28
  • 1
    @blackened: This works for me: box={\mymath[after=\vspace{1cm}]}
    – Ignasi
    Apr 11, 2017 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, 2017 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, 2017 at 18:12
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

5
  • 1
    Forgot to mention: the 1-st argument is a label for the equation, the 2-nd one is the emphasized equation itself.
    – user1999
    Jun 13, 2011 at 13:17
  • The framed package is not required in your answer. The xcolor package is required (maybe with some option to access the lightgreen color). Jun 13, 2011 at 13:48
  • Thanks Gonzalo. It appears, however, that the \empheq package is needed, see here: ctan.mackichan.com/macros/latex/contrib/mh/empheq.pdf
    – user1999
    Jun 13, 2011 at 19:04
  • Of course empheq is needed. I only wanted to point out that framed is not needed but xcolor is. Jun 13, 2011 at 19:06
  • How do you get a border around it? Nov 2, 2015 at 6:46
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.

1
  • 11
    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, 2016 at 12:59

You must log in to answer this question.

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