TikZ: prevent overlapping boxes when using fit

graphicstikz-pgftikz-styles

I am drawing the following figure where I am highlighting a node-set using red. It is meant to include five nodes and it does. But this is a complex shape for fit (probably too complex) so I decided to use two boxes and combine them. Problem is that this saturates the colour on the node where they overlap (Z_1). I want the red colour to be uniform across the highlight box.

enter image description here

MWE:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}
    \tikzset{
        -Latex,auto,node distance =1 cm and 1 cm,semithick,
        state/.style ={ellipse, draw, minimum width = 0.7 cm},
        point/.style = {circle, draw, inner sep=0.04cm,fill,node contents={}},
        bidirected/.style={Latex-Latex,dashed},
        el/.style = {inner sep=2pt, align=left, sloped}
    }

\begin{document}

        \begin{tikzpicture}[node distance =1cm and 1cm]
            
            % F boxes
            \node (F) [label=above:{$F$},fill=gray,fit=(X0) (Z0) (Y0) (X1) (Z1) (Y1),inner sep=6, opacity=0.2] {};
            \node (Fprime) [label=right:{$F'$},fill=red,fit=(Z0) (Y0) (Z1) (Y1), inner sep=3, opacity=0.4] {};
            \node (Fprime2) [fill=red,fit=(Z1) (X1), inner sep=3, opacity=0.4] {};
            
            % t = 0
            \node (X0) [label=left:{$X_0$},point];
            \node (Z0) [label=left:{$Z_0$},below of = X0,point];
            \node (Y0) [label=left:{$Y_0$},below of = Z0,point];
        
            \path (X0) edge (Z0);
            \path (Z0) edge (Y0);
            
            % t = 1
            \node (X1) [label=right:{$X_1$}, right = of X0,point];
            \node (Z1) [label=right:{$Z_1$},below of = X1,point];
            \node (Y1) [label=right:{$Y_1$},below of = Z1,point];
        
            \path (X1) edge (Z1);
            \path (Z1) edge (Y1);
            \path[bidirected] (X0) edge (Z1);
            \path[bidirected] (Z0) edge (X1);
            \path[bidirected] (Z0) edge (Y1);
            \path[bidirected] (Y0) edge (Z1);
           
            % Transition 
            \path (X0) edge (X1);
            \path (Z0) edge (Z1);
            
        \end{tikzpicture}
\end{document}

Best Answer

You want to merge those red fit nodes with the same opacity, it can be done using a scope and the transparency group option. See the code below. Nevertheless, when doing that, you still get some dots that are under this red fill. You could use the background library to draw each part on different layers.

transparency group

\documentclass[tikz,border=3.14mm]{standalone}




\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}

\begin{document}

\tikzset{
    -Latex,auto,node distance =1 cm and 1 cm,semithick,
    state/.style ={ellipse, draw, minimum width = 0.7 cm},
    point/.style = {circle, draw, inner sep=0.04cm,fill,node contents={}},
    bidirected/.style={Latex-Latex,dashed},
    el/.style = {inner sep=2pt, align=left, sloped}
}


        \begin{tikzpicture}[node distance =1cm and 1cm]
            
           
            % t = 0
            \node (X0) [label=left:{$X_0$},point];
            \node (Z0) [label=left:{$Z_0$},below of = X0,point];
            \node (Y0) [label=left:{$Y_0$},below of = Z0,point];
            
            
            
            
            
            
            % t = 1
            \node (X1) [label=right:{$X_1$}, right = of X0,point];
            \node (Z1) [label=right:{$Z_1$},below of = X1,point];
            \node (Y1) [label=right:{$Y_1$},below of = Z1,point];
        
             % F boxes
            
            \node (F) [label=above:{$F$},fill=gray,fit=(X0) (Z0) (Y0) (X1) (Z1) (Y1),inner sep=6, opacity=0.2] {};
                
            \begin{scope}[opacity=.4, transparency group]        
                \node (Fprime) [fill=red,fit=(Z0) (Y0) (Z1) (Y1), inner sep=3] {};
                \node (Fprime2) [fill=red,fit=(Z1) (X1), inner sep=3] {};
            \end{scope}
            
            \path (X0) edge (Z0);
            \path (Z0) edge (Y0);
            \path (X1) edge (Z1);
            \path (Z1) edge (Y1);
            \path[bidirected] (X0) edge (Z1);
            \path[bidirected] (Z0) edge (X1);
            \path[bidirected] (Z0) edge (Y1);
            \path[bidirected] (Y0) edge (Z1);
           
            % Transition 
            \path (X0) edge (X1);
            \path (Z0) edge (Z1);
            
        \end{tikzpicture}

\end{document}