# Beamer: highlighting aligned math with overlay

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.

-
Actually, that isn't an MWE. MWEs should contain a \documentclass, the necessary packages and a document environment, so that the code can be copied into a file and compiled directly. –  Torbjørn T. Apr 19 '12 at 16:11
Can you not upgrade to a more recent version of TeXLive? It's going to be hard for anyone here to test a solution against TL2007 so it might be difficult for you to get an answer that is guaranteed to work with TL2007. –  Loop Space Apr 19 '12 at 17:29
AFAIK there's no stable TL2010/2011 package for my distro (FC15), and previously manteiners mentioned unstable TeXLive packages being screwed up, to the limit of usability. Theyre aiming for FC17 which will come out this summer, unfortunately. If there's somethiing more orthodox, i.e. not a whole "language ecosystem", than TkiZ I'd be more than happy to try it out. –  user881430 Apr 19 '12 at 17:50
Do you have 2GB hard drive space? If so, install TeXLive 2011 in /usr/local and ignore the one that came with your distro. That's what I do - I'm on Debian and that's on TL2009. It works with no problems whatsoever. –  Loop Space Apr 19 '12 at 20:36

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:

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
% http://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}


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:

-
thanks anyway, but, as added to the post, I can't get TkiZ to work with my TeXLive 2007.Is there anyhting else you'd suggest? –  user881430 Apr 19 '12 at 16:55
@user881430,try to use TeXLive 2011 –  Pig Cry Apr 20 '12 at 3:09
I added a hint to the original question. Great package! –  Marco Daniel Aug 19 '12 at 12:34
I saw: thanks! :) –  Claudio Fiandrino Aug 19 '12 at 13:11