2

I would like to add a background behind some nodes of an automaton but the background needs to refer to the nodes of the automaton. So I don't know how to draw it "before" drawing the automaton since I need to refer to the nodes identifiers.

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

2 Answers2

1

If understand what you want then one option is set a low opacity in your \filldraw command:

enter image description here

I have put the opacity at 20%, which is perhaps not enough. Of course, it would be better if the nodes were not shaded at all.

Here is your (modified) 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}

EDIT

Another, and probably better, option is to use the backgrounds library:

enter image description here

To be honest, I can't see much difference between the two pictures:) Here the corresponding code:

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

I have put the background layer inside its' own scope environment -- and I have dropped your scope environment as it's unnecessary (but no doubt needed in your full code).

1

A variation to Andrew answer. I suggest to use positioning library and positioning syntax right=of s instead of depreciated right of=s and instead nodes use labeling paths by help of quotes library. Since purpose of scope is not clear to me, I omit it. Also the state style is changed and colors of background fill is emphasized (made more visible):

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

enter image description here

Zarko
  • 296,517