# How to highlight a pair of terms with compact, rectangular, semi-transparent, colored regions?

I want to create a GIF animation showing the process of multiplying 2 binomials. Each frame must only show a pair of terms that are highlighted with a compact, rectangular, semi-transparent, colored region each.

My attempt is as follows. I use ovalnode here because I have no idea to get a better node container in PSTricks.

\documentclass[mathserif,12pt]{beamer}
\usepackage{pst-node}
\usepackage{amsmath}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{align*}
\PreviewBorder=20pt\relax

\psset{angle=-90,arm=.25,linecolor=red}

\begin{document}
\abovedisplayskip=0pt\relax
\begin{frame}
%===============
\begin{align*}
(\ovalnode{A0}{x}\ovalnode{A1}{{}-x_1})(\ovalnode{B0}{x}\ovalnode{B1}{{}-x_2}) &= \onslide<2->{x^2} \onslide<3->{{-}x_2x} \onslide<4->{{}-x_1x} \onslide<5->{{}+x_1x_2}
\foreach \x in {0,1}{\foreach \y in {0,1}{\only<\the\numexpr2*\x+\y+2\relax>{\ncbar{A\x}{B\y}\nbput[labelsep=2pt]{\times}}}}
\end{align*}
%===============

\end{frame}
\end{document}


I am not satisfied with this result because there is bad spacing and all nodes are displayed at once rather than one pair per frame.

Do you have any idea to make it better? Solution with TikZ is also welcome, but please make it as simple as possible!

\documentclass[mathserif,12pt]{beamer}
\usepackage{pst-node}

\usepackage[active,tightpage,displaymath]{preview}
\PreviewBorder=20pt\relax
\psset{angle=-90,arm=.25,linecolor=red}

\begin{document}
\abovedisplayskip=0pt\relax
\begin{frame}
$(\ovalnode{A0}{x} \only<1-3>{\rnode{A1}{-x_1}}% \only<4,5>{\rnode{A1}{\psovalbox[linecolor=red,fillcolor=red,opacity=0.5,fillstyle=solid]{-x_1}}} ) (\ovalnode{B0}{x}\ovalnode{B1}{{}-x_2}) = \onslide<2->{x^2} \onslide<3->{{-}x_2x} \onslide<4->{{}-x_1x} \onslide<5->{{}+x_1x_2} \foreach \x in {0,1}{\foreach \y in {0,1}{% \only<\the\numexpr2*\x+\y+2\relax>{\ncbar{A\x}{B\y}\nbput[labelsep=2pt]{\times}}}}$

\end{frame}
\end{document}


a more complex example which uses \onslide instead of \only:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage{amsmath}
\usepackage{pst-node}
\newrgbcolor{lila}{0.6 0.2 0.5}
\newrgbcolor{darkyellow}{1 0.9 0}
\def\xstrut{\vphantom{\frac{(A)^1}{(B)^1}}}
\begin{document}
\psset{nodesep=3pt}

\begin{frame}{}{}
\onslide*<1->{Die Bindungsenergie im Tröpfchenmodell setzt sich aus
folgenden Teilen zusammen:}
\begin{itemize}
\item<3->dem \rnode{b}{Oberflächenanteil}
\item<2->Dem \rnode{a}{Volumenanteil},

\medskip
$\onslide<2->{E = \rnode[t]{ae}{\psframebox*[fillcolor=darkyellow,linestyle=none]{\xstrut a_vA}} +} \onslide<3->{% \rnode[t]{be}{\psframebox*[fillcolor=lightgray,linestyle=none]{\xstrut -a_fA^{2/3}}} +} \onslide<4->{% \rnode[t]{ce}{\psframebox*[fillcolor=green,linestyle=none]{\xstrut -a_c\frac{Z(Z-1)}{A^{1/3}}}} + } \onslide<5->{% \rnode[t]{de}{\psframebox*[fillcolor=cyan,linestyle=none]{\xstrut -a_s\frac{(A-2Z)^2}{A}}} + } \onslide<6->{% \rnode[t]{ee}{\psframebox*[fillcolor=yellow,linestyle=none]{\xstrut E_p}}}$  \medskip
\item<4->dem \rnode{c}{Coulomb-Anteil}
\item<5->der \rnode{d}{Symmetrieenergie}
\item<6->sowie einem \rnode{e}{Paarbildungsbeitrag}.
\end{itemize}
\onslide<2->{\nccurve[angleA=-90,angleB=90]{->}{a}{ae}}
\onslide<3->{\nccurve[angleB=45]{->}{b}{be}}
\onslide<4->{\nccurve[angleB=-90]{->}{c}{ce}}
\onslide<5->{\nccurve[angleB=-90]{->}{d}{de}}
\onslide<6->{\nccurve[angleB=-90]{->}{e}{ee}}

\end{frame}
\end{document}


With TikZ, you basically can stuff every math thing in a node and reference it in another TikZ picture that is overlayed and has the remember picture option.

But this has two downsides:

1. Math-mode settings are forgotten; this includes the style (display/text/script/scriptscript) as well as possible horizontal spacing that gets lost because you stuff a binary symbol (-) in a node.

(Granted, the style problem could be solved with \mathpalette/\mathchoice but this will still execute the node creation for every style. If you name all four nodes the same you only get to reference the last (scriptscript) one which probably isn’t typeset anyway. Of course, I can give every one of those four nodes a different name but would have to choose the correct name later while referencing.)

2. It does not work across arbitrary borders. The hf-tikz shows a good example where a column of values in an array environment are highlighted and annotated. This would not be possible with one node.

Using tikzmark (which is used by hf-tikz) allows us to by-pass any math-mode settings as the content is actually typeset in the original environment. Furthermore, it is possible to reference a mark before its occurrence which makes it easy to draw TikZ stuff behind non-TikZ stuff (limited, but still).

The big downside of all this is: We hardly know the dimensions of the contents we want to highlight. (For one-liners, this may be tackled with saving \the\fam.) This is the reason for the existence of offsets settings in hf-tikz and my hl/… values. For further reference on this topic see also hf-tikz doesn't box around equation.

# hf-tikz (without lines)

The hf-tikz is a great package but not very flexible in my eyes. It is very hard to reference the drawn rectangle as one of the (untransformed) corner is saved as a PDF mark and the other (untransformed) corner is a node (!).

The first rectangle created with \tikzmarkin{a} and \tikzmarkend{a} has the corners (pic cs:a) and (a). But those corners are the corners before the offset values are applied.

I’d rather go here with a manual approach.

## Code

\documentclass[mathserif,12pt]{beamer}
\usepackage[customcolors]{hf-tikz}
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt={#1{}{invisible}}},
alt/.code args={<#1>#2#3}{\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}},
}
\colorlet{tikz@hl@color@a}{blue!30}           \colorlet{tikz@hl@color@b}{red!30}
\colorlet{tikz@hl@color@c}{green!75!black!30} \colorlet{tikz@hl@color@d}{orange!30}
\tikzset{
set border color=none,
@set style/.style={#1/.style={set fill color=tikz@hl@color@#1}},
@set style/.list={a,b,c,d},
Fill/.style args={#1/#2}{
path picture={\tikzset{sharp corners}
\path[fill=tikz@hl@color@#1] (path picture bounding box.south west) --
(path picture bounding box.north east)-|(path picture bounding box.south west)
--cycle;
\path[fill=tikz@hl@color@#2] (path picture bounding box.south west) --
(path picture bounding box.north east)|-(path picture bounding box.south west)
--cycle;}}}
\newcommand<>{\tm}[3][]{%
\,\tikzmarkin[#1, visible on={#4}]{#2}#3\tikzmarkend{#2}\,}
\begin{document}
\abovedisplayskip=0pt\relax
\begin{frame}
\begin{align*}
(\tm<1-2,5>[a]{a}{x}-\tm<3-4,5>[b]{b}{x_1})(\tm<1,3,5>[c]{c}{x}-\tm<2,4,5>[d]{d}{x_2})
& = \tm<1,5>[Fill=a/c, above offset=1em]{xx}{x^2}
\uncover<2->{- \tm<2,5>[Fill=a/d]{x2x}{x_2 x}
\uncover<3->{- \tm<3,5>[Fill=b/c]{x1x}{x_1 x}
\uncover<4->{+ \tm<4,5>[Fill=b/d]{x1x2}{x_1 x_2}}}}
\end{align*}
\end{frame}
\end{document}


# TikZ(mark)

This solution needs

For further reference on how to simulate \ncbar in TikZ see Is there a TikZ equivalent to the PSTricks \ncbar command?

## Code

\documentclass[mathserif,12pt]{beamer}
\usepackage{tikz}\usetikzlibrary{tikzmark,paths.ortho}
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt={#1{}{invisible}}},
alt/.code args={<#1>#2#3}{\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}}}
\colorlet{tikz@hl@color@a}{blue!30}
\colorlet{tikz@hl@color@b}{red!30}
\colorlet{tikz@hl@color@c}{green!75!black!30}
\colorlet{tikz@hl@color@d}{orange!30}
\tikzset{
@set style/.style={#1/.style={fill=tikz@hl@color@#1}}, @set style/.list={a,b,c,d},
Fill/.style args={#1/#2}{
path picture={\tikzset{sharp corners}
\path[fill=tikz@hl@color@#1] (path picture bounding box.south west) --
(path picture bounding box.north east)-|(path picture bounding box.south west)
--cycle;
\path[fill=tikz@hl@color@#2] (path picture bounding box.south west) --
(path picture bounding box.north east)|-(path picture bounding box.south west)
--cycle;}},
hl/left/.initial  =+-.1em,
hl/top/.initial   =+.75em,
hl/bottom/.initial=+-.3em,
hl/right/.initial =+.1em,
m edge/.style={rounded corners=3pt, draw=none,
to path={
([shift={(\pgfkeysvalueof{/tikz/hl/left},\pgfkeysvalueof{/tikz/hl/top})}]
\tikztostart) rectangle coordinate (#1-m)
([shift={(\pgfkeysvalueof{/tikz/hl/right},\pgfkeysvalueof{/tikz/hl/bottom})}]
\tikztotarget) coordinate (#1-e!) \tikztonodes
}},
udlr/du distance=.25cm,
dudu/.style args={#1:#2:#3}{
insert path={
(#1-m|-#1-e!) du coordinate (#3-aux) (#2-m|-#2-e!)
(#3-aux) du (#3-m|-#3-e!)}}
}
\newcommand<>{\tm}[3][]{%
\tikz[remember picture,overlay]
\path[style/.expanded={\ifx\\#4\\\else visible on={#4}\fi}]% poor man's ifempty
(0,0) edge[m edge/.try={#2}, #1] (pic cs:#2-e);%
#3\pgfmark{#2-e}}
\begin{document}
\abovedisplayskip=0pt\relax
\begin{frame}
\begin{align*}
(\tm<1-2,5>[a]{a}{x}-\tm<3-4,5>[b]{b}{x_1})(\tm<1,3,5>[c]{c}{x}-\tm<2,4,5>[d]{d}{x_2})
& = \tm<1,5>[Fill=a/c, hl/top=1em]{xx}{x^2}
\uncover<2->{- \tm<2,5>[Fill=a/d]{x2x}{x_2 x}
\uncover<3->{- \tm<3,5>[Fill=b/c]{x1x}{x_1 x}
\uncover<4->{+ \tm<4,5>[Fill=b/d]{x1x2}{x_1 x_2}}}}
\begin{tikzpicture}[remember picture, overlay, rounded corners]
\draw<1>[->] [dudu=a:c:xx];  \draw<2>[->] [dudu=a:d:x2x];
\draw<3>[->] [dudu=b:c:x1x]; \draw<4>[->] [dudu=b:d:x1x2];
\end{tikzpicture}
\end{align*}
\end{frame}
\end{document}