Update matrix entries in beamer
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}
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.