[Tex/LaTex] Nested TikZ nodes

nodestikz-pgf

This isn’t the first time this question was asked but the other solutions don’t work in my case.

Consider the following image:

screenshot

It was created using this code:

\begin{tikzpicture}[
  node distance=7mm,
  title/.style={font=\fontsize{6}{6}\color{black!50}\ttfamily},
  typetag/.style={rectangle, draw=black!50, font=\scriptsize\ttfamily}
]
  \node (decomp) [title] { Decomposition };
  \draw [draw=black!50] (decomp.north west) rectangle +(2.8cm, -2.7cm);

  \node (di) [typetag, below=of decomp.west, anchor=west, xshift=2mm] { Independent };
  \node (dr) [typetag, below=of di.west, anchor=west] { Reduction };
  \node (dnc) [typetag, below=of dr.west, anchor=west] { DivideAndConquer };

  \node (dep) at (3cm, 0) [title] { Dependency };
  \draw [draw=black!50] (dep.north west) rectangle ($(dep.north east) - (0, 2cm)$);

  \node (da) [typetag, below=of dep.west, anchor=west, xshift=2mm] { Atomic };
  \node (dr) [typetag, below=of da.west, anchor=west] { Range };
\end{tikzpicture}

Needless to say, this code is horribly complicated and has no automation (for example, the width of the borders isn’t auto-computed to fit all elements, but rather put in by hand via painstaking trial and error).

There must be an easier way to achieve this.

In particular, I have two questions:

  1. How do I automate the drawing of the border + title (the grey text) so that the border is just large enough to accomodate all the typetag elements that follow after (and how to specify them?)
  2. How can I arrange the typetag elements more smartly? (having to write, every time, below=of Element.west, anchor=west is extremely annoying, but I don’t succeed to put this into the typetag/.style because TikZ “forgets” this information (I have also tried adding ever node/.style={anchor=west} in the tikzpicture options, to no avail).

Question 1 is the important thing here, though. Question 2 is more of a general question that crops up again and again …

Best Answer

For the first point, you can use the fit library.

For the second: According to the manual, setting below also sets anchor=north. So adding anchor=west to typetag and then setting typetag after below does the trick.

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}

\begin{tikzpicture}[
  node distance=7mm,
  title/.style={font=\fontsize{6}{6}\color{black!50}\ttfamily},
  typetag/.style={rectangle, draw=black!50, font=\scriptsize\ttfamily, anchor=west}
]
  \node (decomp) [title] { Decomposition };

  \node (di) [below=of decomp.west, typetag, xshift=2mm] { Independent };
  \node (dr) [below=of di.west, typetag] { Reduction };
  \node (dnc) [below=of dr.west, typetag] { DivideAndConquer };

  \node [draw=black!50, fit={(decomp) (di) (dr) (dnc)}] {};

  \node (dep) at (3cm, 0) [title] { Dependency };

  \node (da) [below=of dep.west, typetag, xshift=2mm] { Atomic };
  \node (dr) [below=of da.west, typetag] { Range };

  \node [draw=black!50, fit={(dep) (dr) (da)}] {};
\end{tikzpicture}
\end{document}

(inner sep, should probably be adjusted a bit)