# How to put box around a single equation in align environment?

I want to put a box around one of my equations in aligned environment. I am looking to do something similar to Attractive Boxed Equations, but the code only caters for individual equations.

Here is MWE. I want to add box around last equation:

\documentclass{beamer}
\usepackage{amsmath}
\begin{document}
\begin{frame}
\frametitle {MWE:  Align two set of equations}
\begin{align*}
\sigma^2&=(a+b)^2-(a+c)^2 \\
&=(a^2+b^2-2ab)-(a^2+c^2-2ac)\\
\intertext{Cancel out common terms, then}
\sigma^2&= b^2-c^2+2(ac-ab)
\end{align*}
\end{frame}
\end{document}


You can used the \Aboxed command, from empheq, which can bow a single equation.

I adapted the code to define a \Ashaded and a Acolorboxed command, which use shadecolour, bgcolour and rulecolour parameters, to be defined with \colorlet.

\documentclass[x11names]{beamer}
\usepackage{empheq}

\makeatletter
\newcommand\Ashaded[1]{\let\bgroup{\romannumeral-}\@Ashaded#1&&\ENDDNE}
\ifnum0={}\fi \setbox \z@
\hbox{$\displaystyle#1{}\m@th$\kern\fboxsep \kern\fboxrule }%
\edef\@tempa {\kern \wd\z@ &\kern -\the\wd\z@ \fboxsep
\the\fboxsep }\@tempa \colorbox{shadecolour}{$#1#2$}%
}
\colorlet{bgcolour}{LavenderBlush1!50}
\colorlet{rulecolour}{Plum4}
\newcommand\Acolorboxed[1]{\let\bgroup{\romannumeral-}\@Acolorboxed#1&&\ENDDNE}
\def\@Acolorboxed#1&#2&#3\ENDDNE{%
\ifnum0={}\fi \setbox \z@
\hbox{$\displaystyle#1{}\m@th$\kern\fboxsep \kern\fboxrule }%
\edef\@tempa {\kern \wd\z@ &\kern -\the\wd\z@ \fboxsep
\the\fboxsep\fboxrule \the\fboxrule }\@tempa \fcolorbox{rulecolour}{bgcolour}{$#1#2$}%
}
\makeatother

\begin{document}

\begin{frame}
\frametitle {MWE: Align two set of equations}
\begin{align*}
\Acolorboxed{\sigma^2&=(a+b)^2-(a+c)^2} \\
&=(a^2+b^2-2ab)-(a^2+c^2-2ac)\\
\intertext{Cancel out common terms, then}
\end{align*}
\end{frame}

\end{document}


• Thanks for posting great reply. I have one question regarding colorlet. I copied your code as such but I am getting errors for \colorlet{shadecolour}{Thistle3!50} and similar lines. Do I need to define these colors before using them? I am assuming these are not default colors like red,blue, etc – NAASI Jun 16 '16 at 20:32
• Look at the whole code: I loaded the beamer class with option x11names (which indeed is a xcolor option). The names are defined and demonstrated in the xcolor documentation. – Bernard Jun 16 '16 at 20:47

Here is a cheating way with an \rlap, requiring manual estimation of box width, which will work for any number of alignment tabs in the equation:

\documentclass{beamer}
\usepackage{amsmath}
\begin{document}
\begin{frame}
\frametitle {MWE:  Align two set of equations}
\begin{align*}
\sigma^2&=(a+b)^2-(a+c)^2 \\
&=(a^2+b^2-2ab)-(a^2+c^2-2ac)\\
\intertext{Cancel out common terms, then}
\rlap{\textcolor{blue!20}{\rule[-\dp\strutbox]{130pt}{\baselineskip}}}\,
\sigma^2&= b^2-c^2+2(ac-ab)
\end{align*}
\end{frame}
\end{document}


If only one alignment tab is present the \Aboxed macro from mathtools can do it.

\documentclass{beamer}
\usepackage{amsmath,stackengine,mathtools}
\begin{document}
\begin{frame}
\frametitle {MWE:  Align two set of equations}
\begin{align*}
\sigma^2&=(a+b)^2-(a+c)^2 \\
&=(a^2+b^2-2ab)-(a^2+c^2-2ac)\\
\intertext{Cancel out common terms, then}
\Aboxed{\sigma^2&= b^2-c^2+2(ac-ab)}
\end{align*}
\end{frame}
\end{document}


To get color there, Torbjorn provides a method at Highlight an equation within an align environment, in which he started with the \Aboxed definition and changed the \boxed to \fcolorbox.

• Manual calculation is what I am trying to avoid. \Aboxed would be a better choice in that case. Though Nothing fancy ... – NAASI Jun 16 '16 at 16:50
• But the problem with \Aboxed is that it does not include the equation number in box. How can we get that feature? – NAASI Jun 16 '16 at 16:51
• @NAASI The \Aboxed and torbjorn's adaptation works across one alignment tab. For your example, that would be suitable. If you have multiple tabs, though, it will not work. Likewise, your desire for the equation number to be included in the box is likewise out of reach of the \Aboxed approaches. – Steven B. Segletes Jun 16 '16 at 16:53
• to avoid manual calculation, at least in this example, \hphantom{...} would work quite well. – barbara beeton Jun 16 '16 at 17:26
• @barbarabeeton Yes, for this example it would. But two comments before your own, the OP expressed desire for the box to include the equation number (for align not aligned). In that case, the \hphantom would likewise be of little use. – Steven B. Segletes Jun 16 '16 at 17:54