[Tex/LaTex] How to overlay tikz matrix in beamer

beameroverlaystikz-matrixtikz-pgf

Background: I encounter some unexpected situations while overlaying a tikz matrix in Beamer. The latex code is given at the end (note: you can also read and copy it at ShareLatex:TikzMatrixOverlay). There are two tikz matrices and the first one without overlay is for comparison. The second matrix with overlay is not satisfying in the following ways:

  1. The |[red]| instruction for the (2,1) cell (second row, first column) does not work; notice that the |[red]| instruction for the (3,2) cell does work.
  2. I cannot replace the \only<4>{4444 & 4444 & 4444} \\ by \only<4>{4444 & 4444 & 4444 \\}. Otherwise, I will get the Missing \endgroup inserted error.
  3. If I use \only<4>{4444 & 4444 & 4444} \\ (as the source code indicated), an extra empty cell (in the fourth row) is shown during the overlay.

Notice that you can also see the comments in source code and the image below for the three problems. tikz-matrix-overlay

Problem: Therefore, my problem is how to fix these three problems. Specifically,

  1. To enable the |[red]| instruction for the (2,1) cell;
  2. To eliminate the extra empty cell (in the fourth row) during the overlay;
  3. To overlay the tikz matrix row by row.
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}[fragile]{TikzMatrixOverlay}
  \begin{tikzpicture}

  % matrix without overlay for comparison
  \matrix [align = center, matrix of nodes, nodes = {text width = 16mm, draw}]
  {
    1 & 1 & 1 \\
    |[red]| 22 & 22 & 22 \\
    333 & |[red]| 333 & 333 \\
    4444 & 4444 & 4444 \\
  };

  % matrix with overlay
  \matrix [align = center, xshift = 6.0cm, matrix of nodes, nodes = {text width = 16mm,   draw}]
  {
    1 & 1 & 1 \\
    \only<2->{|[red]| 22 & 22 & 22 \\} % Problem 1: the |[red]| instruction does not work.
    \only<3->{333 & |[red]| 333 & 333 \\} % this |[red]| instruction does work.
    \only<4>{4444 & 4444 & 4444} \\   % Problem 2: it cannot be: \only<4>{4444 & 4444 &     4444 \\}
  };
  \end{tikzpicture}
\end{frame}

\end{document}

Best Answer

Very recently, yesterday, I sent to CTAN a new package called aobs-tikz which is designed exactly for these jobs. It is based on Daniel's method illustrated in Mindmap tikzpicture in beamer (reveal step by step) and it extends the styles I defined in Highlighting in Beamer using TikZ nodes.

A preview of its usage:

\documentclass{beamer}
\usepackage{lmodern,tikz}
\usetikzlibrary{overlay-beamer-styles,matrix}

\begin{document}

\begin{frame}[fragile]{TikzMatrixOverlay}
  \begin{tikzpicture}[
  background default draw=black, % define default behaviour
  background default text=black, % define default behaviour
  background default aspect={solid}, % define default behaviour
  highlight/.style={background draw=red, % define modified behaviour
    background text=red, % define modified behaviour
    background aspect=dashed,% define modified behaviour
    },
  ]

  % matrix without overlay for comparison
  \matrix [align = center, matrix of nodes, nodes = {text width = 16mm, draw},]
  {
    1 & 1 & 1 \\
    |[red]| 22 & 22 & 22 \\
    333 & |[red]| 333 & 333 \\
    4444 & 4444 & 4444 \\
  };

  % matrix with overlay
  \matrix [align = center, xshift = 6.0cm, matrix of nodes, nodes = {text width = 16mm,draw},
  row 4/.style={visible on=<3>}% original visible on style by Daniel
  ]{
    1 & 1 & 1 \\
    |[highlight, draw on=<1->, text on=<1->, aspect on=<3>]| 22 & 22 & 22 \\ % new styles
    333 & |[highlight, draw on=<2->, text on=<2->, aspect on=<3>]| 333 & 333 \\ % new styles
    4444 & 4444 & 4444\\
  };

  \end{tikzpicture}  
\end{frame}

\end{document}

enter image description here