# Animation with math crossed out or circled

I'm trying to make an animation to show how to find the median of a small list of numbers. As I would incorporate it into a presentation, I used beamer frames, but I'm open to alternatives. I used tikz because I usually like the output of tikz pictures.

I've hit some difficulties. I built the frames one by one, so the approach is tedious and doesn't scale up well. I tried a for loop, but didn't get there. Because of my lack of familiarity with tikz, boxes move around a bit and the numbers get misaligned. It would be great to fix the wobbling and have a loop that would make it possible to generate an animation for other sets of numbers.

I also had a plan to add a little bubble at the end to explain that the median of an even list of numbers is, by convention, taken to be the mean of the last two numbers "in the middle", but that's still a to-do. Now I don't think my animation is too ugly, but I don't think it's too pretty either, so feel free to use your aesthetic judgement if you see obvious improvements.

So we have a sorted list and cancel out the extremes two by two until:

My lame code:

\PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
\documentclass[20pt]{beamer}
\setbeamertemplate{footline}{}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\newcommand{\crossout}[1]{\raisebox{-0.5mm}{%
\tikz{\draw(0,0) node[anchor=west, inner sep=0pt](cross){#1};
\draw[cross out, red, line width=2pt](cross.north west) -- (cross.south east);
\draw[cross out, red, line width=2pt](cross.south west) -- (cross.north east);
}}}

\newcommand\selected[1]{\raisebox{-0.5mm}{
\tikz[baseline=(center.base)] \node[draw,rectangle,blue,line width=2pt,inner sep=2pt] (center){#1};
}}

\usepackage{mathtools}
\usepackage{empheq}
\usepackage[many]{tcolorbox}
\tcbset{highlight math style={enhanced,
colframe=red!60!black,colback=yellow!50!white,arc=4pt,boxrule=1pt,
}}

\newcommand{\s}{\hspace{10pt}}
\begin{document}
\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
1 \s 1 \s 2 \s 2 \s 3 \s 3 \s 5 \s 6 \s 6 \s 8 \s 8 \s 9
\end{empheq}
\end{frame}

\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
\crossout{1} \s 1 \s 2 \s 2 \s 3 \s 3 \s 5 \s 6 \s 6 \s 8 \s 8 \s \crossout{9}
\end{empheq}
\end{frame}

\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
\crossout{1} \s \crossout{1} \s 2 \s 2 \s 3 \s 3 \s 5 \s 6 \s 6 \s 8 \s \crossout{8} \s \crossout{9}
\end{empheq}
\end{frame}

\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
\crossout{1} \s \crossout{1} \s \crossout{2} \s 2 \s 3 \s 3 \s 5 \s 6 \s 6 \s \crossout{8} \s \crossout{8} \s \crossout{9}
\end{empheq}
\end{frame}

\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
\crossout{1} \s \crossout{1} \s \crossout{2} \s \crossout{2} \s 3 \s 3 \s 5 \s 6 \s \crossout{6} \s \crossout{8} \s \crossout{8} \s \crossout{9}
\end{empheq}
\end{frame}

\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
\crossout{1} \s \crossout{1} \s \crossout{2} \s \crossout{2} \s \crossout{3} \s 3 \s 5 \s \crossout{6} \s \crossout{6} \s \crossout{8} \s \crossout{8} \s \crossout{9}
\end{empheq}
\end{frame}

\begin{frame}
\begin{empheq}[box=\tcbhighmath]{align*}
\crossout{1} \s \crossout{1} \s \crossout{2} \s \crossout{2} \s \crossout{3} \selected{3 \s 5} \crossout{6} \s \crossout{6} \s \crossout{8} \s \crossout{8} \s \crossout{9}
\end{empheq}
\end{frame}

\end{document}


POST SCRIPTUM

A minor tweak on Mike's excellent answer. I found it useful to load the makecellpackage to allow linebreaks inside a node:

\usepackage{makecell}
...
\makecell[c]{%
by convention, \\
the median is: \\
$\dfrac{3+5}{2} = 4$}


I also added the option align=center inside the node to center the above text. And ended up selecting xframe.south for the "callout absolute pointer."

I also found that I could keep the standard beamer font to, say, 11pt, to keep the frametitle to a standard size, while controlling the font size inside the tikzpictures, with something like \tikzset{font={\fontsize{20pt}{30}\selectfont}}. Nothing to write home about, but here it is for the record.

In this solution the box is drawn with TikZ, which prevents it from changing its size. The command \crossoutframes will produce all the frames, the last one with a bubble.

The parameters:

1. (optional, shown with defaults)
• xnum distance=20pt: distance between numbers (center to center)
• xnum count=12: number of numbers in list
• xnum steps=5: how many numbers to cross out from left and right
2. comma seperated list of numbers
3. text for bubble

With \crossoutframes{1,1,2,2,3,3,5,6,6,8,8,9}{by convention} this produces (last frame)

Or with \crossoutframes[xnum distance=15pt,xnum count=15,xnum steps=6]{1,1,2,2,3,3,4,5,5,6,6,8,8,9,9}{what now?} (odd number of numbers)

The code:

\PassOptionsToPackage{usenames,dvipsnames,svgnames}{xcolor}
\documentclass[20pt]{beamer}
\setbeamertemplate{footline}{}

\usepackage{tikz}
\usetikzlibrary{shapes.misc}
% for bubble
\usetikzlibrary{shapes.callouts}
% for frame around last two numbers
\usetikzlibrary{fit}

% some parameters
\tikzset{%
% distance between numbers, center to center
xnum distance/.store in=\xnumdistance,
% number of numbers in list
xnum count/.store in=\xnumcount,
% how many numbers to cross out from left and right
xnum steps/.store in=\xnumsteps
}

\newcommand{\crossoutframes}[3][xnum distance=20pt,xnum count=12,xnum steps=5]{%
% setup
\tikzset{#1}
% two runs more for framing last 2 numbers and bubble
\foreach \step in {0,1,...,\numexpr\xnumsteps+2}{%
\centering
\begin{frame}
\begin{tikzpicture}[x=1pt,y=1pt,line width=2pt,inner sep=0pt,outer sep=0pt,remember picture]
% frame (drawing manually prevents changing size)
% old:    colframe          colback              arc                 boxrule
\filldraw[draw=red!60!black,fill=yellow!50!white,rounded corners=4pt,line width=1pt]
(0,20) -- (\xnumcount*\xnumdistance+\xnumdistance,20) -- (\xnumcount*\xnumdistance+\xnumdistance,-20) -- (0,-20) -- cycle;
% going through the numbers
\foreach \xnum [count=\i] in {#2}{%
% limit \step to 'xnum steps'
\ifnum\step>\xnumsteps\edef\step{\xnumsteps}\fi
% run 0: nothing crossed out
\ifnum\step=0\relax
% number not crossed out
\node[shape=cross out] (x\i) at (\i*\xnumdistance,0) {\xnum};
\else
%cross out or not, from left and right
\ifnum\i>\step
\ifnum\i<\numexpr\xnumcount-\step+1\relax
% number not crossed out
\node[shape=cross out] (x\i) at (\i*\xnumdistance,0) {\xnum};
\else
% number crossed out
\node[shape=cross out,draw=red] (x\i) at (\i*\xnumdistance,0) {\xnum};
\fi
\else
% number crossed out
\node[shape=cross out,draw=red] (x\i) at (\i*\xnumdistance,0) {\xnum};
\fi
\fi
}
% frame around last 2 numbers
\ifnum\step>\xnumsteps
\edef\firstnode{x\the\numexpr\xnumsteps+1}%
\edef\secondnode{x\the\numexpr\xnumcount-\xnumsteps}%
\node[draw=blue,inner sep=3pt,fit=(\firstnode) (\secondnode)] (xframe) {};
%      \node[draw=blue,inner sep=3pt,fit=(x6) (x7)] (xframe) {};
\fi
\end{tikzpicture}

\hspace*{150pt}
\begin{tikzpicture}[line width=2pt,remember picture,overlay]
% bubble
\ifnum\step>\numexpr\xnumsteps+1\relax
\node[draw=green,inner sep=3pt,line join=round,
shape=ellipse callout,callout absolute pointer={(xframe.south east)}] {#3};
\fi
\end{tikzpicture}
\end{frame}
}
}

\begin{document}
\crossoutframes{1,1,2,2,3,3,5,6,6,8,8,9}{by convention}

\crossoutframes[xnum distance=15pt,xnum count=15,xnum steps=6]{1,1,2,2,3,3,4,5,5,6,6,8,8,9,9}{what now?}
\end{document}