[Tex/LaTex] How to modify a node in TikZ when using beamer’s overlays

beamermatricestikz-pgf

I have a matrix initialized with some colors and no values. With the help of beamer's overlays I wanted to modify the matrix in the next step, changing some of the colors and inserting some values. I attach a figure, so you can get a clear picture of what I want to achieve (on the top you have the empty matrix, and on the bottom the matrix after it has been filled in):

enter image description here

I have tried to overwrite the whole matrix with a new one, but that has several problems:

  • Text like "Some label" gets overwritten and looks thicker in the second slide.
  • If I need to do N steps, instead of just 2, rewriting the whole matrix every time is really tedious.

I know I am not really aware of all the possibilities in TikZ and Beamer, so there might something there that I am missing, and that would allow me to do what I want. Does anyone know what can I do in order to update the matrix nodes between the overlay steps?

Best Answer

Here is a very simple method (inspired by my own answer to Choosing styles conditionally in TikZ).

Result

progressive matrix

Code

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{frame}[fragile]
  \frametitle{Progressive matrix}

  \tikzset{
    % some styles for nodes
    orange node/.style={draw,fill=orange!50,text=black},
    gray node/.style={draw,fill=gray!50,text=black},
    gray empty node/.style={draw,fill=gray!50,text=gray!50},
    red node/.style={draw,fill=red!50,text=blue},
    % styles for each overlay
    my styles 1/.style={
      nodes={gray empty node},
    },
    my styles 2/.style={
      nodes={gray empty node},
      column 1/.style={nodes=gray node},
    },
    my styles 3/.style={
      nodes={orange node},
      column 1/.style={nodes=gray node},
    },
    my styles 4/.style={
      nodes={orange node},
      column 1/.style={nodes=gray node},
      row 2 column 2/.style={nodes=red node},
    },
  }

  % apply overlay's styles
  \only<1>{\tikzset{my styles/.style={my styles 1}}}
  \only<2>{\tikzset{my styles/.style={my styles 2}}}
  \only<3>{\tikzset{my styles/.style={my styles 3}}}
  \only<4>{\tikzset{my styles/.style={my styles 4}}}

  \centering
  \begin{tikzpicture}
    \matrix[matrix of nodes,
    nodes={minimum width=2cm,minimum height=2cm,font=\bfseries},
    column sep=-\pgflinewidth,row sep=-\pgflinewidth,my styles]{
      1 & 2 & 3 \\
      4 & 5 & 6 \\
      7 & 8 & 9 \\
    };
  \end{tikzpicture}

\end{frame}
\end{document}