How to draw a block on others with Tikz?

Viewed 3099

Here is the diagram I am trying to make using tikz. Blocks A, B and C are inside a block (here, block with red background) and blocks E and F are inside another block. The two huge blocks (with red background) have labels on top (LabA and LabB). Moreover, I would like block E to be right and in the middle of the blocks A and B. And arrows going to block E should have some bending angle; so, not like in the one I have tried.

Expected

Here is my code.

\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\tikzstyle{block} = [draw=black, thick, text width=2cm, minimum height=1cm, align=center]  
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

    \begin{tikzpicture}
    
        \node[block] (a) {a};
        \node[block, below=of a, yshift=0.7cm] (b) {b};
        \node[block, below=of b, yshift=0.7cm] (c) {c};
        \node[block, right=of c, xshift=0.7cm] (d) {d};
        
        \node[block, right=of a, xshift=0.7cm, yshift=-0.4cm] (e) {e};
        
        \draw [arrow] (a) -- (e);
        \draw [arrow] (b) -- (e);
        \draw [arrow] (c) -- (d);
        
    \end{tikzpicture}

\end{document}

How could I add a rectangle over some nodes and write a label on top of it.
How to have the arrows here with a certain angle of inclination (not straight), and also to have the E block in the middle and in right of the A and B blocks. I though I could get the arrows with \draw [arrow] (a) -|- (e);, but it does not work.
Thank you in advance

Current state

1 Answers

I gave it a try in the hope of getting the requirements right:

\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\tikzstyle{block} = [draw=none, thick, text width=.4cm, minimum height=.5cm, align=center, fill=blue!50]  
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}


    \begin{tikzpicture}
    \Large
        %red rectangles    
        \node (r1) [draw=none, fill=red, minimum width=1.2cm,minimum height=3.5cm]{};
        \node (r2) [right=2cm of r1.center, anchor=center, draw=none, fill=red, minimum width=1.2cm,minimum height=3.5cm]{};
        \node (r1Label)[above=0cm of r1] {\textbf{LabA}};
        \node (r2Label)[above=0cm of r2]{\textbf{LabB}};
    
        %nodes
        \node[block, below=3mm of r1.north, anchor=north] (a) {\color{white}A};
        \node[block, below=4mm of a] (b) {\color{white}B};
         \node[block, below=4mm of b] (c) {\color{white}C};
        \node[block, right=2cm of c.center, anchor=center] (d) {\color{white}D};
        \node at ($(a.south)+(2,-.2)$) [block] (e) {\color{white}E};
        
        %arrows
        \draw [arrow, rounded corners=2] (a) -- ($(a)+(1,0)$) |- ($(e.west)+(0,.1)$);
        \draw [arrow, rounded corners=2] (b) -- ($(b)+(1,0)$) |- ($(e.west)+(0,-.1)$);
        \draw [arrow] (c) -- (d);
        
    \end{tikzpicture}

\end{document}

With this result:

latex output

Some explanations:

  • You can just draw rectangles in the back by defining them before the other nodes. There are more sophisticated ways using layers, but I think in this example it suffices.
  • I mostly used absolute distances like in the given example code. You may have to adjust... However, using these you can easily find out the middle between A and B (distance 4mm) to find the y-Position of E (a.south+ 2mm). Using ($(node1)+(node2)$) you can calculate node positions.
  • The -|- option you mentioned works in two steps. First draw -- to some anchor point (otherwise this is ambiguous), then |- to the target node.
  • The rounded corners option for draw takes the rounding radius as a parameter.
Related