[Tex/LaTex] How to draw a diagram with boxes of text inside each other and have them laid out automatically

diagramstikz-pgf

How do I make a diagram of the following structure where each element is a box with text and possibly sub-elements.

Here is an example of the desired structure. The sizes and alignment of the boxes are arbitrary.
Example

I want the diagram to automatically align the boxes so that there is a bit of space between each.

The order of the boxes does not matter, only which ones are inside which.

Nice to have features:

  • Text should be optional if the box contain other boxes
  • The background color of each box can be specified.

I'd prefer a TikZ solution, but other ways are good too if they do not depend on external programs and the structure would be easier to keep track of.

Best Answer

The description of the problem is still somehow vague, but perhaps something like this could help:

\documentclass{article}
\usepackage{tikz}

\newcommand\TBox[3][]{%
  \tikz\node[draw,ultra thick,text width=#2,align=left,#1] {#3};}

\begin{document}

\TBox{5cm}{%
  \TBox[fill=brown]{3cm}{\TBox[fill=red!30]{1cm}{B}\hfill\TBox[fill=cyan!30]{1cm}{C}} \\[1ex]
  \TBox[fill=green!30]{1cm}{D}\quad\TBox[fill=yellow!30]{1cm}{E}}

\end{document}

enter image description here

\Tbox has three arguments: the (first) optional argument can be used to pass options to the rectangular \node for the box; the second argument specifies the text width, and the third argument is used for the contents.

After the comments, this can be closer to the desired result; now \TBox has one optional argument (to pass options to the node) and one mandatory argument (the contents of the box):

\documentclass{article}
\usepackage{tikz}

\newcommand\TBox[2][]{%
  \tikz\node[draw,ultra thick,align=left,#1] {#2};\hskip2pt}

\begin{document}

\TBox{%
  \TBox[fill=brown]{Some test text \\ \TBox[fill=red!30]{Some}\TBox[fill=cyan!30]{A}} \\
  \TBox[fill=green!30]{B}\TBox[fill=yellow!30]{Text}}

\end{document}

enter image description here