TikZ:使用 fit 时防止框重叠

TikZ:使用 fit 时防止框重叠

我正在绘制下图,其中我用红色突出显示一个节点集。它应该包含五个节点,而且确实如此。但这是一个复杂的形状(可能太复杂了),所以我决定使用两个框并将它们组合起来。问题是这会使它们重叠的节点(Z_1)上的颜色饱和。我希望红色在整个突出显示框中保持一致。

在此处输入图片描述

梅威瑟:

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

答案1

您想要合并具有相同不透明度的红色拟合节点,可以使用范围和选项来完成transparency group。请参阅下面的代码。然而,这样做时,您仍然会得到一些点在下面用红色填充。您可以使用background库在不同的图层上绘制每个部分。

透明集团

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

答案2

您可以在节点周围绘制路径,并且可以通过一些透明填充进行注释:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc, chains,
                decorations,
                fit,
                positioning,
                shapes}
\tikzset{
 node distance = 1 cm and 1 cm,
    arr/.style = {-Latex, semithick},
    alr/.style = {Latex-Latex, dashed},
  point/.style = {circle,  draw, fill, 
                  inner sep=0.04cm, node contents={}},
every label/.append style = {inner sep=1mm, font=\footnotesize},
        }

\begin{document}
    \begin{tikzpicture}[
node distance = 1cm and 1cm,
  start chain = going below,
                        ]
% t = 0, t = 1
    \foreach \i [count=\ii] in {X, Z, Y}
{
\node (\i0) [point, on chain, label=left:{$\i_0$}];
\node (\i1) [point, right=of \i0, label=right:{$\i_1$}];
}
\draw[arr]
        (X0) edge (Z0) (Z0) edge (Y0)
        (X1) edge (Z1) (Z1) edge (Y1) 
% Transition
        (X0) edge (X1) 
        (Z0) edge (Z1);
% Cross
\draw[alr] 
        (X0) edge (Z1) 
        (Z0) edge (X1) 
        (Z0) edge (Y1) 
        (Y0) edge (Z1);
         
% F boxes
\node (F) [inner sep=6, fill=gray, opacity=0.2,
           label=$F$, 
           fit=(X0) (Y1),] {};
\fill[red!50, semitransparent]
        ($(X1)+ (-2mm,2mm)$) -| ($(Y1)+ (2mm,-2mm)$) -|
        ($(Z0)+ (-2mm,2mm)$) -|  cycle;

    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容