# How to modify a node in TikZ when using beamer's overlays

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):

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?

-
You should add what you have so far; it will be easier to spot the problem. –  egreg Sep 17 '12 at 21:55
add comment

## 2 Answers

Here's a possible solution using a comma-separated lists; the list \elist lets you specify the entries for the matrix and the fill color for the cells; you invoke \entries and \centries to populate the matrix and to fill the cells, respectively:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\definecolor{myora}{RGB}{235,97,61}

\newcounter{entry}
\newcounter{tmp}

\loop\ifnum\value{entry}<9
\stepcounter{entry}
\expandafter\def\csname entry\romannumeral\value{entry}\endcsname{}
\repeat

\setcounter{entry}{0}
\loop\ifnum\value{entry}<9
\stepcounter{entry}
\expandafter\def\csname centry\romannumeral\value{entry}\endcsname{}
\repeat

\makeatletter
\def\entries{%
\setcounter{tmp}{0}
\@for\temp@a:=\elist\do
{\stepcounter{tmp}
\expandafter\edef\csname entry\romannumeral\value{tmp}\endcsname{\temp@a}
}
}

\def\centries{%
\setcounter{tmp}{0}
\@for\temp@a:=\elist\do
{\stepcounter{tmp}
\expandafter\edef\csname centry\romannumeral\value{tmp}\endcsname{\temp@a}
}
}
\makeatother

\def\elist{\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1},\noexpand\phantom{1}}\entries

\begin{document}

\begin{frame}

\only<1>{
\def\elist{gray!50,gray!50,gray!50,gray!50,gray!50,gray!50,gray!50,gray!50,gray!50}\centries
}
\only<2>{
\def\elist{3,6,9,12,15,18,21,24,27}\entries
\def\elist{gray!50,myora,myora,gray!50,myora,myora,gray!50,myora,myora}\centries
}
\only<3>{
\def\elist{2,4,6,8,10,12,14,16,18}\entries
\def\elist{myora,gray!50,myora,myora,gray!50,myora,myora,gray!50,myora}\centries
}
\only<4>{
\def\elist{-1,-2,-3,-4,-5,-6,-7,-8,-9}\entries
\def\elist{myora,myora,gray!50,myora,myora,gray!50,myora,myora,gray!50}\centries
}
\only<5>{
\def\elist{-3,-6,-9,-12,-15,-18,-21,-24,-27}\entries
\def\elist{myora,gray!50,myora,gray!50,myora,gray!50,myora,gray!50,myora}\centries
}
\only<6>{
\def\elist{1,0,1,0,1,0,1,0,1}\entries
\def\elist{gray!50,myora,gray!50,myora,gray!50,myora,gray!50,myora,gray!50}\centries
}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes,every node/.style={draw,text width=2cm,align=center},ampersand replacement=\&,nodes in empty cells] (mat)
{
|[fill=\centryi]|\entryi \& |[fill=\centryii]|\entryii \& |[fill=\centryiii]|\entryiii \\
|[fill=\centryiv]|\entryiv \& |[fill=\centryv]|\entryv \& |[fill=\centryvi]|\entryvi \\
|[fill=\centryvii]|\entryvii \& |[fill=\centryviii]|\entryviii \& |[fill=\centryix]|\entryix \\
};
\node[anchor=south] at (mat.north) {Some Label};
\end{tikzpicture}\par
\end{frame}

\end{document}


An animation of the resulting document:

-
add comment

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

Result

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}

-
add comment