0

I'm using the fit option to draw a rectangle that fits a few nodes but when I add the fill option to change the color of this rectangle the nodes inside disappear.

\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{ positioning, fit}

\tikzset{       
    smallRect/.style={very thick,draw,rectangle},
    smallElli/.style={very thick,draw,circle}
}   

\begin{document}
    
    \begin{tikzpicture}[]
        \node (hk) [smallElli] {$n_k$};
        \node (start)[below=1mm of hk] {Start};
        \node (vk1) [smallRect, right=of hk] {$\mathnormal{n}^k_1$};
        \node (vk2) [smallRect, right=of vk1] {$\mathnormal{n}^k_2$};
        \node (vk3) [smallRect, right=of vk2] {$\mathnormal{n}^k_3$};
        
        \node (null) [smallRect, right=of vk3,inner sep=2mm] {Null};
        
        \node (vk2a) [smallRect, above=of vk2] {$\mathnormal{n}^2_1$};
        \node (vk2b) [smallRect, below=of vk2] {$\mathnormal{n}^2_2$};
        
        \node (fitI2) [draw,densely dashed,inner sep=8pt,fit={(vk2a) (vk2b)}] {};
        \node (fitI3) [draw,densely dashed,inner sep=4pt,fit={(hk) (null)}] {};
    \end{tikzpicture}
    
\end{document}

enter image description here

with \node (fitI3) [draw,fill=green!20,densely dashed,inner sep=4pt,fit={(hk) (null)}] {};

enter image description here

I found a solution in this Example using pgfdeclarelayer, and it worked perfectly. Now I'm looking if there is a simple and quick way to do this.

% Inspired by this Example: https://texample.net/tikz/examples/system-combination/
% Harish K Krishnamurthy <www.ece.neu.edu/~hkashyap/>
\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{ positioning, fit}

\tikzset{       
    smallRect/.style={very thick,draw,rectangle},
    smallElli/.style={very thick,draw,circle}
}   

\begin{document}
    \pgfdeclarelayer{background}
    \pgfdeclarelayer{foreground}
    \pgfsetlayers{background,main,foreground}
    
    \begin{tikzpicture}[]
        \node (hk) [smallElli] {$n_k$};
        \node (start)[below=1mm of hk] {Start};
        \node (vk1) [smallRect, right=of hk] {$\mathnormal{n}^k_1$};
        \node (vk2) [smallRect, right=of vk1] {$\mathnormal{n}^k_2$};
        \node (vk3) [smallRect, right=of vk2] {$\mathnormal{n}^k_3$};
        
        \node (null) [smallRect, right=of vk3,inner sep=2mm] {Null};
        
        \node (vk2a) [smallRect, above=of vk2] {$\mathnormal{n}^2_1$};
        \node (vk2b) [smallRect, below=of vk2] {$\mathnormal{n}^2_2$};
        
        \node (fitI2) [draw,densely dashed,inner sep=8pt,fit={(vk2a) (vk2b)}] {};
        
        
        \begin{pgfonlayer}{background}
            \path (start.west |- hk.north)+(-0.5,0.3) node (a) {};
            
            \path (null.east |- null.south)+(+0.5,-0.7) node (c) {};
            
            \path[fill=green!20,rounded corners, draw=black!50, dashed]
            (a) rectangle (c);     
            
        \end{pgfonlayer}
    \end{tikzpicture}
    
\end{document}

enter image description here

4
  • Well, using such a background layer is actually the way to go. What exactly is your question? Jul 13, 2022 at 14:45
  • 1
    Is there any other simple way to do it than using pgfdeclarelayer?
    – user220507
    Jul 13, 2022 at 14:48
  • 2
    I think, this is already a very straight-forward way to do this: First draw the nodes, then draw the background that frames these code. What is perceived as "easy" can be very different between different people. I fear, alternative approaches where the code is shorter would also be a bit more complicated from the logical perspecitve. Jul 13, 2022 at 15:12
  • The only thing I can see is to use a fit node also for the green one, adding an inner sep to make it looser, but I do not think it can get easier that this...
    – Rmano
    Jul 13, 2022 at 15:35

2 Answers 2

0

This solution is not always valid, but this nodes distribution can be constructed with a matrix. And as a matrix is a node, we can fill and draw it at the same time of its inner nodes.

\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
    
     \begin{tikzpicture}[%
        mymatrix/.style={matrix of math nodes, draw, densely dashed, fill=green!30, inner sep=8pt, column sep=8mm, row sep=8mm, nodes={anchor=center, draw, very thick, inner sep=.3333em, solid, fill=none}},
      ]
     \matrix (vk) [mymatrix]{%
     |[circle]| n_k & n^k_1 & n^k_2 & n^k_3 & \text{Null}\\
     };
     \matrix (vk2) [mymatrix, fill=none, row sep=10mm] at (vk-1-3) {n_1^2 \\ n^k_2 \\ n_2^2\\};
     \node[below] at (vk.south-|vk-1-1.center) {Start};
    \end{tikzpicture}
   
\end{document}

enter image description here

1
  • This is what I was looking for. thank you so much.
    – user220507
    Jul 13, 2022 at 17:26
1

If you like this:

enter image description here

change the last line of your code (before \end{tikzpicture}) in:

\node (fitI3) [fill=green,opacity=.3,draw,densely dashed,inner sep=4pt,fit={(hk) (null)}] {};
1
  • Thank you for your answer, But the fill color still affects the colors of the nodes inside the rectangle. I just want to change the background color without affecting the foreground.
    – user220507
    Jul 13, 2022 at 16:16

You must log in to answer this question.