TikZ node within another

like this :

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes, positioning}

\begin{document}

\begin{tikzpicture}[node distance=4cm]
\node [name=n1, rectangle, minimum width=5cm, minimum height=3cm,
text centered, text width=3cm, draw=black] {};

\node [name=t, rectangle, minimum width=5cm, minimum height=1.5cm,
text centered, text width=3cm, below=(0mm of n1.north)] {The whole thing};

\node [name=n2,rectangle, minimum width=3cm, minimum height=1.5cm,
text centered, text width=3cm, anchor=south, above=(0cm of n1.south),
draw=black,fill=lightgray] {A part of it};
\end{tikzpicture}

\end{document}

enter image description here


Another alternative with a unique node defined as a matrix and matrix of nodes:

\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[%
    part/.style={draw, fill=black!30},
    whole/.style={
        matrix,
        draw,
        matrix of nodes,
        inner xsep=3mm,
        inner ysep=-.5\pgflinewidth,
        nodes={minimum height=12mm},
        row 2/.style={nodes={part}}
    }]

\node[whole] (A) {
The whole thing\\
A part of it\\
};
\end{tikzpicture}
\end{document}

enter image description here


An alternative solution:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{fit,shapes,positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance=0mm, 
every node/.style = {rectangle, inner sep=0mm, outer sep=0mm,
                     align=center, minimum height=15mm},  
                    ]
\node (t) [text width=5cm] {The whole thing};
\node (b) [text width=3cm,draw,fill=lightgray, 
           below=of t] {A part of it};
\node [draw,fit=(t) (b)]    {};
    \end{tikzpicture}
\end{document}

Tags:

Tikz Pgf