在 Tikz pgfonlayer 中更改已经绘制的节点颜色

在 Tikz pgfonlayer 中更改已经绘制的节点颜色

我想更改幻灯片 2 中节点 q1 的颜色,但不重新绘制同一节点。有没有办法使用 Tikzset 来更改它?我不确定如何通过节点名称(在本例中为 q1)“调用”节点并更改其颜色。最后,我想使用 pdf 输出制作 GIF。

\documentclass[hyperref={pdfpagelabels=false}]{beamer}
%\usepackage{amssymb}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows.meta,calc,cd,animations,shapes}
\usetikzlibrary{patterns}
\usetikzlibrary{datavisualization}
\tikzset{
->, % makes the edges directed
>=stealth, % makes the arrow heads bold
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
initial text=$ $, % sets the text that appears on the start arrow
}
%\usepackage{animate}

\begin{document}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\begin{frame}
    \begin{figure}
        \begin{tikzpicture}[node distance = 2cm, on grid,>={Stealth[inset=0pt,length=6pt,angle'=28,round]}][overlay]
    
        \node[state, initial] at (0,0) (q1) {$q^1$};
        \node[state] at (1,-2.5) (q2) {$q^2$};
        \node[state, right of=q2] (q3) {$q^3$};
        \node[state, right of=q3] (q4) {$q^4$};
        \node[state, right of=q4] (q5) {$q^5$};
        \node[state,accepting] at (8,0) (q6) {$q^6$};
        
        
        \node[state] at (2,0) (q7) {$q^7$};
        \node[state, right of=q7] (q8) {$q^8$};
        \node[state] at (6,0) (q9) {$q^9$};
        
        \node[state] at (1,2.5) (q10) {$q^{10}$};
        \node[state, right of=q10] (q11) {$q^{11}$};
        \node[state, right of=q11] (q12) {$q^{12}$};
        \node[state, right of=q12] (q13) {$q^{13}$};
        
        \node[state,fill=red!20, densely dashed] at (2,-1.25) (q14) {$q^{14}$};
        \node[state,fill=red!20, densely dashed] at (4,-1.25) (q15) {$q^{15}$};
        \node[state,fill=red!20, densely dashed] at (2,1.25) (q16) {$q^{16}$};
        \node[state,fill=red!20, densely dashed] at (6,1.25) (q17) {$q^{17}$};
    
        \draw 
        
       (q1) edge[bend right] node [below left]{$e^1$} (q2)
       (q2) edge node [below] {$e^2$} (q3)
       (q3) edge node [below]{$e^3$} (q4)
       (q4) edge node [below]{$e^4$} (q5)
       (q5) edge[bend right] node [below right] {$e^5$} (q6)
       
       (q1) edge node [above]{$e^6$} (q7)
       (q7) edge node [above]{$e^7$} (q8)
       (q8) edge node [above]{$e^8$} (q9)
       (q9) edge node [above]{$e^9$} (q6)
       
       (q1) edge[bend left] node [below left]{$e^{10}$} (q10)
       (q10) edge node [above]{$e^{11}$} (q11)
       (q11) edge node [above]{$e^{12}$} (q12)
       (q12) edge node [above]{$e^{13}$} (q13)
       (q13) edge[bend left] node [above right]{$e^{14}$} (q6)
       
       (q2) edge[bend right, densely dashed] node [above left]{$e^{15}$} (q14)
       (q14) edge[bend right, densely dashed] node [ right]{$e^{16}$} (q7)
       (q4) edge[bend right, densely dashed] node [ right]{$e^{17}$} (q15)
       (q15) edge[bend right, densely dashed] node [ right]{$e^{18}$} (q9)
       (q12) edge[bend left=10, densely dashed] node [ above]{$e^{19}$} (q16)
       (q16) edge[bend left=10, densely dashed] node [ above right]{$e^{20}$} (q9)
       (q12) edge[bend left=10, densely dashed] node [ above right]{$e^{21}$} (q17)
       (q17) edge[bend left, densely dashed] node [ above right]{$e^{22}$} (q9);

       \begin{pgfonlayer}{main}
                \draw<1->;
                \only<2->{
                \node [state,fill=yellow] at (0,0) (q1) {$q^1$};
                };
                \draw<3-> (q7) to (q10);
        \end{pgfonlayer}
    \end{tikzpicture}
    \end{figure}
\end{frame}

\end{document}

答案1

您可以使用该overlay-beamer-styles库来更改节点的样式:

\documentclass[hyperref={pdfpagelabels=false}]{beamer}
%\usepackage{amssymb}
%\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows.meta,calc,cd,animations,shapes}
\usetikzlibrary{patterns}
\usetikzlibrary{datavisualization}
\usetikzlibrary{overlay-beamer-styles}
\tikzset{
->, % makes the edges directed
>=stealth, % makes the arrow heads bold
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
initial text=$ $, % sets the text that appears on the start arrow
}
%\usepackage{animate}

\begin{document}

\begin{frame}
    \begin{figure}
        \begin{tikzpicture}[node distance = 2cm, on grid,>={Stealth[inset=0pt,length=6pt,angle'=28,round]}][overlay]
    
        \node[state, initial,alt=<2->{fill=yellow}{}] at (0,0) (q1) {$q^1$};
        \node[state] at (1,-2.5) (q2) {$q^2$};
        \node[state, right of=q2] (q3) {$q^3$};
        \node[state, right of=q3] (q4) {$q^4$};
        \node[state, right of=q4] (q5) {$q^5$};
        \node[state,accepting] at (8,0) (q6) {$q^6$};
        
        
        \node[state] at (2,0) (q7) {$q^7$};
        \node[state, right of=q7] (q8) {$q^8$};
        \node[state] at (6,0) (q9) {$q^9$};
        
        \node[state] at (1,2.5) (q10) {$q^{10}$};
        \node[state, right of=q10] (q11) {$q^{11}$};
        \node[state, right of=q11] (q12) {$q^{12}$};
        \node[state, right of=q12] (q13) {$q^{13}$};
        
        \node[state,fill=red!20, densely dashed] at (2,-1.25) (q14) {$q^{14}$};
        \node[state,fill=red!20, densely dashed] at (4,-1.25) (q15) {$q^{15}$};
        \node[state,fill=red!20, densely dashed] at (2,1.25) (q16) {$q^{16}$};
        \node[state,fill=red!20, densely dashed] at (6,1.25) (q17) {$q^{17}$};
    
        \draw 
        
       (q1) edge[bend right] node [below left]{$e^1$} (q2)
       (q2) edge node [below] {$e^2$} (q3)
       (q3) edge node [below]{$e^3$} (q4)
       (q4) edge node [below]{$e^4$} (q5)
       (q5) edge[bend right] node [below right] {$e^5$} (q6)
       
       (q1) edge node [above]{$e^6$} (q7)
       (q7) edge node [above]{$e^7$} (q8)
       (q8) edge node [above]{$e^8$} (q9)
       (q9) edge node [above]{$e^9$} (q6)
       
       (q1) edge[bend left] node [below left]{$e^{10}$} (q10)
       (q10) edge node [above]{$e^{11}$} (q11)
       (q11) edge node [above]{$e^{12}$} (q12)
       (q12) edge node [above]{$e^{13}$} (q13)
       (q13) edge[bend left] node [above right]{$e^{14}$} (q6)
       
       (q2) edge[bend right, densely dashed] node [above left]{$e^{15}$} (q14)
       (q14) edge[bend right, densely dashed] node [ right]{$e^{16}$} (q7)
       (q4) edge[bend right, densely dashed] node [ right]{$e^{17}$} (q15)
       (q15) edge[bend right, densely dashed] node [ right]{$e^{18}$} (q9)
       (q12) edge[bend left=10, densely dashed] node [ above]{$e^{19}$} (q16)
       (q16) edge[bend left=10, densely dashed] node [ above right]{$e^{20}$} (q9)
       (q12) edge[bend left=10, densely dashed] node [ above right]{$e^{21}$} (q17)
       (q17) edge[bend left, densely dashed] node [ above right]{$e^{22}$} (q9);

    \end{tikzpicture}
    \end{figure}
\end{frame}

\end{document}

在此处输入图片描述

(该 .gif 是使用 ImageMagick 创建的convert -delay 30 -loop 0 -density 100 -alpha remove "document".pdf "document".gif

相关内容