我想在自动机的某些节点后面添加背景,但背景需要引用自动机的节点。所以我不知道如何在绘制自动机“之前”绘制它,因为我需要引用节点标识符。
\documentclass[margin=5mm]{standalone}
\usepackage{tikz} \usetikzlibrary{arrows,automata,calc}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=20mm,semithick,
information text/.style={rounded corners=1pt,inner sep=1ex},font=\scriptsize]
\begin{scope}
\node[initial,initial where=left,initial text=,initial distance=4mm,
accepting,state,draw=black,fill=orange!15] (s) {$s_0$};
\node[accepting,state,draw=black,fill=yellow!4] (t)[right of=s] {$t_0$};
\node[state,draw=black,fill=yellow!4] (s1)[below of=s] {$s_1$};
\node[accepting,state,draw=black,fill=orange!15] (t1)[right of=s1] {$t_1$};
\path (s) edge[bend angle=25,bend left] node {$a$} (t)
(t) edge node[below=2pt] {$b$} (s)
(s1) edge[bend angle=25,bend left] node {$a$} (t1)
(t1) edge node[below=2pt] {$b$} (s1)
(s) edge[densely dashed] node[left=2pt] {$c$} (s1)
(t) edge[densely dashed] node[right=2pt] {$d$} (t1);
\filldraw[fill=cyan!20,draw=cyan!20,rounded corners=5pt] ($(s.north)+(0,0.25)$) -- ($(s.west)+(-0.25,0)$) -- ($(t1.south)+(0,-0.25)$)-- ($(t1.east)+(0.25,0)$) -- ($(t.east)+(0.25,0)$) -- ($(t.north)+(0,0.25)$) node {};
\end{scope}
\end{tikzpicture}
\end{document}
答案1
如果您明白自己想要什么,那么一个选项就是在命令中设置低不透明度\filldraw
:
我把不透明度设为 20%,这可能还不够。当然,如果节点完全没有阴影就更好了。
这是您的(修改后的) MWE:
\documentclass[margin=5mm]{standalone}
\usepackage{tikz} \usetikzlibrary{arrows,automata,calc}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=20mm,semithick,
information text/.style={rounded corners=1pt,inner sep=1ex},font=\scriptsize]
\begin{scope}
\node[initial,initial where=left,initial text=,initial distance=4mm,
accepting,state,draw=black,fill=orange!15] (s) {$s_0$};
\node[accepting,state,draw=black,fill=yellow!4] (t)[right of=s] {$t_0$};
\node[state,draw=black,fill=yellow!4] (s1)[below of=s] {$s_1$};
\node[accepting,state,draw=black,fill=orange!15] (t1)[right of=s1] {$t_1$};
\path (s) edge[bend angle=25,bend left] node {$a$} (t)
(t) edge node[below=2pt] {$b$} (s)
(s1) edge[bend angle=25,bend left] node {$a$} (t1)
(t1) edge node[below=2pt] {$b$} (s1)
(s) edge[densely dashed] node[left=2pt] {$c$} (s1)
(t) edge[densely dashed] node[right=2pt] {$d$} (t1);
\filldraw[fill=cyan!2,opacity=0.2,draw=cyan!20,rounded corners=5pt] ($(s.north)+(0,0.25)$) -- ($(s.west)+(-0.25,0)$) -- ($(t1.south)+(0,-0.25)$)-- ($(t1.east)+(0.25,0)$) -- ($(t.east)+(0.25,0)$) -- ($(t.north)+(0,0.25)$) node {};
\end{scope}
\end{tikzpicture}
\end{document}
编辑
另一个可能更好的选择是使用该backgrounds
库:
说实话,我看不出这两张图片有什么区别:)这里是相应的代码:
\documentclass[margin=5mm]{standalone}
\usepackage{tikz} \usetikzlibrary{arrows,automata,calc,backgrounds}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=20mm,semithick,
information text/.style={rounded corners=1pt,inner sep=1ex},font=\scriptsize]
\node[initial,initial where=left,initial text=,initial distance=4mm,
accepting,state,draw=black,fill=orange!15] (s) {$s_0$};
\node[accepting,state,draw=black,fill=yellow!4] (t)[right of=s] {$t_0$};
\node[state,draw=black,fill=yellow!4] (s1)[below of=s] {$s_1$};
\node[accepting,state,draw=black,fill=orange!15] (t1)[right of=s1] {$t_1$};
\path (s) edge[bend angle=25,bend left] node {$a$} (t)
(t) edge node[below=2pt] {$b$} (s)
(s1) edge[bend angle=25,bend left] node {$a$} (t1)
(t1) edge node[below=2pt] {$b$} (s1)
(s) edge[densely dashed] node[left=2pt] {$c$} (s1)
(t) edge[densely dashed] node[right=2pt] {$d$} (t1);
\begin{scope}[on background layer]
\filldraw[fill=cyan!2,draw=cyan!20,rounded corners=5pt]
($(s.north)+(0,0.25)$) -- ($(s.west)+(-0.25,0)$) -- ($(t1.south)+(0,-0.25)$)
-- ($(t1.east)+(0.25,0)$) -- ($(t.east)+(0.25,0)$) -- ($(t.north)+(0,0.25)$) node {};
\end{scope}
\end{tikzpicture}
\end{document}
我已将该background
层放入它自己的scope
环境中 - 并且我已删除您的scope
环境,因为它是不必要的(但毫无疑问在您的完整代码中是需要的)。
答案2
安德鲁答案的变体。我建议使用positioning
库和定位语法right=of s
,而不是贬值right of=s
,而是节点借助quotes
库使用标签路径。由于scope
我不清楚其目的,我省略了它。此外,state
样式也发生了变化,背景填充的颜色也得到了强调(变得更加明显):
\documentclass[tikz, margin=5mm]{standalone}
\usetikzlibrary{arrows,automata,backgrounds,calc, positioning,quotes}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,
node distance=20mm, semithick,
information text/.style={rounded corners=1pt,inner sep=1ex},
state/.append style={draw=black,fill=#1},
font=\scriptsize]
\node[initial,initial text=,initial distance=4mm,
accepting,state=orange!15] (s) {$s_0$};
\node[accepting,state=yellow!4] (t) [right=of s] {$t_0$};
\node[state=yellow!4] (s1)[below=of s] {$s_1$};
\node[accepting,state=orange!15] (t1)[right=of s1] {$t_1$};
\path (s) edge[bend angle=25,bend left, "$a$"] (t)
(t) edge["$b$"] (s)
(s1) edge[bend angle=25,bend left,"$a$"] (t1)
(t1) edge["$b$"] (s1)
(s) edge[densely dashed,"$c$" '] (s1)
(t) edge[densely dashed,"$d$"] (t1);
\scoped[on background layer]
\filldraw[fill=cyan!20,draw=cyan,rounded corners=8pt]
($( s.north)+(0, 0.25)$) -- ($(s.west)+(-0.25,0)$) --
($(t1.south)+(0,-0.25)$) -- ($(t1.east)+(0.25,0)$) --
($(t.east)+(0.25,0)$) -- ($(t.north)+(0,0.25)$);
\end{tikzpicture}
\end{document}