如何更改 TikZ 节点填充和文本颜色

如何更改 TikZ 节点填充和文本颜色

投影仪演示,我观察到演讲者有一个系统的 TikZ 图,并且在下一瞬间,节点填充颜色发生了变化(在 3:08)。

我知道

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

用于给节点添加填充颜色、文本颜色。

我的问题是:你好改变在投影机演示期间动态地更改节点填充颜色或文本颜色?

其他查询在 SE 点上改变边缘颜色,使用

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

首先,我提供一个借来的TikZ 图。

% 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}

实施建议:\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}

答案1

您可以通过 来实现它only<>

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

您必须将这些行放在节点(state)放置之前。

在此处输入图片描述

完整代码

\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}

相关内容