1

In a beamer presentation, I observed that the speaker has a TikZ diagram of a system and in the next instant, the node fill color changes (at 3:08).

I am aware that

\tikzstyle{every state}=[fill=red,draw=none,text=white]

is used to add fill color, text color to the node.

My query is: How do you change the node fill color, or text color dynamically during a beamer presentation?

Other queries on SE point to changing color of edges, using

\only<2>{\color{red}}

To begin with, I am providing a MWE of a borrowed TikZ diagram.

% Author: Till Tantau
% Source: The PGF/TikZ manual
%http://texample.net/tikz/examples/feature/automata-and-petri-nets/

\documentclass{article}

\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\usepackage{verbatim}

\begin{document}

\begin{comment}
:Title: State machine
:Tags: Manual, Automata, Graphs

Another examle from the manual.

| Author: Till Tantau
| Source: The PGF/TikZ manual

\end{comment}

 \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick]
%         \tikzstyle{every state}=[fill=red,draw=none,text=white]             
  \tikzstyle{every state}=[draw=none]

  \node[initial,state] (A)                    {$q_a$};
 \node[state]         (B) [above right of=A] {$q_b$};
 \node[state]         (D) [below right of=A] {$q_d$};
 \node[state]         (C) [below right of=B] {$q_c$};
\node[state]         (E) [below of=D]       {$q_e$};

\path (A) edge              node {0,1,L} (B)
        edge              node {1,1,R} (C)
    (B) edge [loop above] node {1,1,L} (B)
        edge              node {0,1,L} (C)
    (C) edge              node {0,1,L} (D)
        edge [bend left]  node {1,0,R} (E)
    (D) edge [loop below] node {1,1,R} (D)
        edge              node {0,1,R} (A)
    (E) edge [bend left]  node {1,0,R} (A);
\end{tikzpicture}

\end{document}

Implementing the suggestion: \only<1>{\tikzstyle{every state}=[draw=none]} \only<2->{\tikzstyle{every state}=[fill=red,draw=none,text=white]}

\documentclass[11pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usetheme{default}
\begin{document}    
\begin{frame}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node   distance=2.8cm,
 semithick]         
\tikzstyle{every state}=[draw=none]
\node[initial,state] (A)                    {$q_a$};
\node[state]         (B) [above right of=A] {$q_b$};
\node[state]         (D) [below right of=A] {$q_d$};
\node[state]         (C) [below right of=B] {$q_c$};
\node[state]         (E) [below of=D]       {$q_e$};
\only<1>{\tikzstyle{every state}=[draw=none]} \only<2->{\tikzstyle{every state}=[fill=red,draw=none,text=white]}
\path (A) edge              node {0,1,L} (B)
  edge              node {1,1,R} (C)
 (B) edge [loop above] node {1,1,L} (B)
   edge              node {0,1,L} (C)
  (C) edge              node {0,1,L} (D)
  edge [bend left]  node {1,0,R} (E)
 (D) edge [loop below] node {1,1,R} (D)
 edge              node {0,1,R} (A)
 (E) edge [bend left]  node {1,0,R} (A);
 \end{tikzpicture}
 \end{frame}
 \end{document}
  • As mentioned in other posts, you can use only<>. Try this: \only<1>{\tikzstyle{every state}=[draw=none]} \only<2->{\tikzstyle{every state}=[fill=red,draw=none,text=white]} – nidhin Nov 6 at 7:25
  • I seem to be missing something, I have updated the query based on your answer – GermanShepherd Nov 6 at 8:10
  • what’s the output that you are getting? – nidhin Nov 6 at 8:40
  • The color change doesn't happen in the frames. – GermanShepherd Nov 6 at 8:51
  • What do you mean by 'in the frames'? The way changes in Beamer work is by producing a new page of the PDF for each change. So if a node is blue and then changes to red, say, you need 2 pages - one with a tikzpicture with the node blue and one with a tikzpicture with the node red. This is, needless to say, extremely inefficient in terms of compilation. – cfr Nov 7 at 23:43
2

You can achieve it with only<>.

\only<1>{\tikzset{every state/.style={draw=none}}}
\only<2->{\tikzset{every state/.style={fill=red,draw=none,text=white}}}

You have to put these lines before the node (state) placement.

enter image description here

Complete code

\documentclass[11pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usetheme{default}
\begin{document}    
\begin{frame}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,semithick]         

\only<1>{\tikzset{every state/.style={draw=none}}}
\only<2->{\tikzset{every state/.style={fill=red,draw=none,text=white}}}

\node[initial,state] (A)                {$q_a$};
\node[state]         (B) [above right of=A] {$q_b$};
\node[state]         (D) [below right of=A] {$q_d$};
\node[state]         (C) [below right of=B] {$q_c$};
\node[state]         (E) [below of=D]       {$q_e$};

\path 
  (A) edge              node {0,1,L} (B)
      edge              node {1,1,R} (C)
  (B) edge [loop above] node {1,1,L} (B) 
      edge              node {0,1,L} (C)
  (C) edge              node {0,1,L} (D)
      edge [bend left]  node {1,0,R} (E)
  (D) edge [loop below] node {1,1,R} (D)
      edge              node {0,1,R} (A)
  (E) edge [bend left]  node {1,0,R} (A);
\end{tikzpicture}
\end{frame}
\end{document}
  • 1
    @cfr thanks for pointing out. edited the answer. – nidhin Nov 8 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.