[Tex/LaTex] Highlight an equation within an align environment

alignboxesequationshighlighting

I would like to define a command that puts a red box around an equation with yellow highlighting. I have the basics working pretty well. What I am unable to figure out is:

  1. How to keep the alignment of the equations on the equal sign.
  2. Be able to have the alignment character within the call to highlight box. That is, I would really prefer to be able to say something like:

    \highlightbox{g &= f}

instead of what I resorted to doing below.

Here is what I have so far:

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}

\newcommand{\highlightbox}[1]{%
    \setlength{\fboxrule}{6pt}\fcolorbox{red}{yellow}{#1}\quad%
}

\begin{document}
\begin{align*}
   \highlightbox{a} &\highlightbox{= b} \\
                 c  &              = d \\
                 e  &              = f  \\
   \highlightbox{g} &\highlightbox{= f} \\
\end{align*}
\end{document}

Best Answer

The mathtools package provides an Aboxed command, that allows one to make a box across an alignment. By redefining that slightly, you can get the desired effect:

\documentclass[11pt]{article}
\usepackage{color}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsfonts}

\makeatletter
\def\@Aboxed#1&#2\ENDDNE{%
  \settowidth\@tempdima{$\displaystyle#1{}$}%
  \addtolength\@tempdima{\fboxsep}%
  \addtolength\@tempdima{\fboxrule}%
  \global\@tempdima=\@tempdima
  \kern\@tempdima
  &
  \kern-\@tempdima
  \fcolorbox{red}{yellow}{$\displaystyle #1#2$}
}
\makeatother

\begin{document}
\begin{align*}
              \Aboxed{a &= b}  &        c  &= d \\
                 c  &    = d   & \Aboxed{i &= k} \\
                 e  &    = f   &        g  &= h  
\end{align*}
\end{document}

I basically copied the definition of Aboxed from mathtools.dtx and changed the last line of the definition from \boxed to \fcolorbox{....


(I wasn't sure if the following should be added as a new answer or as an edit. Please advise if I should make it a new answer.)

When looking at some lecture notes I wrote a while ago, I found that I had a command for this purpose in my preamble, and it came to me that I had copied it from LaTeXcommunity.org, but never used it. So here is another solution, based on this post at LaTeX Community by daleif:

\documentclass{article}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{xcolor}

\newlength\dlf
\newcommand\alignedbox[2]{
  % #1 = before alignment
  % #2 = after alignment
  &
  \begingroup
  \settowidth\dlf{$\displaystyle #1$}
  \addtolength\dlf{\fboxsep+\fboxrule}
  \hspace{-\dlf}
  \fcolorbox{red}{yellow}{$\displaystyle #1 #2$}
  \endgroup
}

\begin{document}
\begin{align*}
              \alignedbox{a}{=b}  &        c  &= d \\
                 c  &    = d   & \alignedbox{i}{=k} \\
                 e  &    = f   &        g  &= h  
\end{align*}
\end{document}

This yields:

enter image description here

This does not have the problem of overwriting an existing command, but requires the calc package.

Related Question