[Tex/LaTex] Beamer: highlighting aligned math with overlay

beamerboxeshighlightingoverlays

This question led to a new package:
hf-tikz

Minimal working example:

\documentclass{beamer}
\usepackage{amsmath}
\begin{document}
 \begin{frame}
 \begin{align}
      a_i + b_j = 10 & \forall i\in I, j\in J \\
      c_j + d_j + a_i >= 30 & \forall i\in I, j\in J
 \end{align}
 \end{frame}
 \end{document}

Suppose I want to highlight both a_i 's in the equations and then the second equation as a whole. I could use \alert<2> on each a_i, but this would not work with the second equation, the part after the & would need to be enclosed in a separated \alert<3> directive. However I have several equations to highlight at once, so I'd prefer a method that works across "groups", i.e across &.

Secondly, if it was possible, I'd prefer, rather to use alert, to enclose the equations in boxes, like with \boxed, and have labels on such boxes, for descriptive purposes.

What packages I need to look into, if any?

EDIT: is there something else beside TikZ (and possibly simpler)? I can't get it to work with my outdated TeXLive 2007, and I can't update the latter either.

Best Answer

By following the method shown in Background coloring with overlay specification in algorithm2e + beamer package, it is possible to build this MWE:

\documentclass{beamer}
\usepackage{amsmath,amssymb}
\usepackage{tikz}
\usetikzlibrary{fit,calc}

\newcommand{\tikzmark}[1]{%
  \tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {};}

\definecolor{mybrown}{RGB}{255,218,195}
\definecolor{myframe}{RGB}{197,122,195}

\usetheme{Copenhagen}

\begin{document}

\section{Mysection}
\subsection{A subsection}
\begin{frame}{The equations}
\begin{block}{}
 \begin{align}
      \tikzmark{a}a_i\tikzmark{b} + b_j = 10 & \forall i\in I, j\in J \\
      \tikzmark{e}c_j + d_j + \tikzmark{c}a_i\tikzmark{d} >= 30 & \forall i\in I, j\in J\tikzmark{f}
 \end{align}

\begin{tikzpicture}[remember picture,overlay]
\coordinate (aa) at ($(a)+(0,0.25)$);
\coordinate (bb) at ($(b)+(0,0)$);
\node<1>[draw=myframe,line width=1pt,fill=mybrown,opacity=0.4,rectangle,rounded corners,fit=(aa) (bb)] {};
\coordinate (cc) at ($(c)+(0,0.25)$);
\coordinate (dd) at ($(d)+(0,0)$);
\node<1>[draw=myframe,line width=1pt,fill=mybrown,opacity=0.4,rectangle,rounded corners,fit=(cc) (dd)] {};
\coordinate (ee) at ($(e)+(0,0.25)$);
\coordinate (ff) at ($(f)+(0,-0.25)$);
\node<2>[draw=myframe,line width=1pt,fill=mybrown,opacity=0.4,rectangle,rounded corners,fit=(ee) (ff)] {};
\end{tikzpicture}
\end{block}
\end{frame}

\end{document}

that allows to get those two frames:

enter image description here

enter image description here

Thus you can select the zone you want to highlight thanks to \tikzmark, an entire equation or just a part displaying it as "alert".

A TikZ-free solution

\documentclass{beamer}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\usepackage{xcolor}

% Code by Gonzalo Medina
% https://tex.stackexchange.com/questions/35319/a-boxed-alternative-with-minimal-spacing/35346#35346
\newbox\FBox
\NewDocumentCommand\Highlight{O{black}O{white}mO{0.5pt}O{0pt}O{0pt}}{%
    \setlength\fboxsep{#4}\sbox\FBox{\fcolorbox{#1}{#2}{#3\rule[-#5]{0pt}{#6}}}\usebox\FBox}

\usetheme{Copenhagen}

\begin{document}

\begin{frame}{The equations}
\only<1>{
\begin{align}
      &\Highlight[blue][blue!20]{$a_i$}[3pt] + b_j = 10  \qquad\forall i\in I, j\in J \\
      &c_j + d_j +\Highlight[blue][blue!20]{$a_i$}[3pt] >= 30  \qquad \forall i\in I, j\in J
\end{align}
}
\only<2>{
\begin{align}
      &a_i + b_j = 10  \qquad \forall i\in I, j\in J \\
      &\Highlight[orange][orange!20]{$c_j + d_j +a_i >= 30  \qquad\forall i\in I, j\in J$}[3pt]
\end{align}
}
\end{frame}


\end{document}

enter image description here

Beware that with the latter method, one can not place tabs (with &) inside the \Highlight command.

Solution with the hf-tikz package

\documentclass{beamer}
\usepackage{lmodern,amsmath,amssymb}
\usepackage[beamer]{hf-tikz}

\usetheme{Copenhagen}

\begin{document}

\section{Mysection}
\subsection{A subsection}
\begin{frame}{The equations}
\begin{block}{}
 \begin{align}
   \tikzmarkin<1>{a}a_i\tikzmarkend{a} + b_j = 10 & \forall i\in I, j\in J \\
   \tikzmarkin<2>{c}c_j + d_j + \tikzmarkin<1>{b}a_i\tikzmarkend{b} >= 30 & \forall i\in I, j\in J\tikzmarkend{c}
 \end{align}
\end{block}
\end{frame}

\end{document}

Notice that it also avoids the so called "jumping effect" visible in the TikZ-free solution:

enter image description here