[Tex/LaTex] Update matrix entries in beamer

beamermath-modematricesoverlays

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}

Best Answer

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}

enter image description here