# Update matrix entries in beamer

I'm trying to show row reducing a matrix in a beamer presentation. I'd like to show successive steps of the row reduction using overlays. The following is close to what I want to do. The problem is that the matrix on the right moves once all of the minus signs disappear from the first column, in overlay 4. I've tried using the methods from section 9.5 of the beamer user guide about "Dynamically Changing Text or Images", but neither overprint nor overlayarea seem to work correctly in an equation environment. Any tips would be great!

\begin{frame}{Row-echelon form}
Reduce the following matrix to row-echelon form.
$\begin{bmatrix} 2 & 1 & -1 & 3 \\ 1 & -1 & 2 & 1 \\ -4 & 6 & -7 & 1 \\ 2 & 0 & 1 & 3 \end{bmatrix} \onslide<2-> \to \begin{bmatrix} 1 & -1 & 2 & 1 \\ \alt<2>{2}{0} & \alt<2>{1}{3} & \alt<2>{-1}{-5} & \alt<2>{3}{1} \\ \alt<2-3>{-4}{0} & \alt<2-3>{6}{2} & \alt<2-3>{-7}{1} & \alt<2-3>{1}{5} \\ \alt<2-4>{2}{0} & \alt<2-4>{0}{2} & \alt<2-4>{1}{-3} & \alt<2-4>{3}{1} \end{bmatrix}$
\action<5>{}
\end{frame}


One possibility is to use array environments with columns of type p{<length>}, instead of bmatrix:

\documentclass{beamer}
\usepackage{array}

\begin{document}

\begin{frame}{Row-echelon form}
Reduce the following matrix to row-echelon form.
$\left[ \begin{array}{*{4}{>{\raggedleft\arraybackslash}p{1.3em}<{}}} 2 & 1 & -1 & 3 \\ 1 & -1 & 2 & 1 \\ -4 & 6 & -7 & 1 \\ 2 & 0 & 1 & 3 \end{array} \right] \onslide<2-> \to \left[ \begin{array}{*{4}{>{\raggedleft\arraybackslash}p{1,3em}<{}}} 1 & -1 & 2 & 1 \\ \alt<2>{2}{0} & \alt<2>{1}{3} & \alt<2>{-1}{-5} & \alt<2>{3}{1} \\ \alt<2-3>{-4}{0} & \alt<2-3>{6}{2} & \alt<2-3>{-7}{1} & \alt<2-3>{1}{5} \\ \alt<2-4>{2}{0} & \alt<2-4>{0}{2} & \alt<2-4>{1}{-3} & \alt<2-4>{3}{1} \end{array} \right]$
\action<5>{}
\end{frame}

\end{document}


• Thanks, @gonzalo-medina. That looks nice. I've never used the array package before. – SMoore Jun 8 '13 at 3:20
• @SMoore please see my updated answer. I fixed math mode inside the cells. – Gonzalo Medina Jun 8 '13 at 13:11
• I thought it looked a little different. Couldn't figure out what it was. Thanks for following up. – SMoore Jun 11 '13 at 1:52

You can avoid that the entries move around by measuring the widths of the two alternatives and using the larger of the two widths. For this I use \mathmakeboxlargestof from this nice answer of Philippe Goutet.

\documentclass{beamer}
\usepackage{calc}
\usepackage{mathtools}
\newlength{\letterwidth}
\newcommand{\mathmakeboxlargestof}[3]{%
\setlength{\letterwidth}{\maxof{\widthof{$#1$}}{\widthof{$#2$}}}%
\mathmakebox[\letterwidth]{\vphantom{#1}\vphantom{#2}#3}%
}
\makeatletter
\newcommand\replace{}
\def\replace<#1>#2#3{%
\alt<#1>{\mathmakeboxlargestof{#2}{#3}{{#2}}}%
{\mathmakeboxlargestof{#2}{#3}{{#3}}}%
}
\makeatother

\begin{document}

\begin{frame}{Row-echelon form}
Reduce the following matrix to row-echelon form.
$\begin{bmatrix} 2 & 1 & -1 & 3 \\ 1 & -1 & 2 & 1 \\ -4 & 6 & -7 & 1 \\ 2 & 0 & 1 & 3 \end{bmatrix} \onslide<2-> \to \begin{bmatrix} 1 & -1 & 2 & 1 \\ \replace<2>{2}{0} & \replace<2>{1}{3} & \replace<2>{-1}{-5} & \replace<2>{3}{1} \\ \replace<2-3>{-4}{0} & \replace<2-3>{6}{2} & \replace<2-3>{-7}{1} & \replace<2-3>{1}{5} \\ \replace<2-4>{2}{0} & \replace<2-4>{0}{2} & \replace<2-4>{1}{-3} & \replace<2-4>{3}{1} \end{bmatrix}$
\action<5>{}
\end{frame}

\end{document}


Note that I use additional braces {} around the entries #2 and #3; otherwise the spacing of the minus signs wouldn't be correct.