TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Did you check the manual for TikZ trees? – percusse May 5 '13 at 18:48
There's nothing obviously right for this, but I might have overlooked it. This is the first time I've tried to do graphics in LaTeX. – LJNielsenDk May 5 '13 at 19:01
Perhaps a duplicate of this: tex.stackexchange.com/q/18600/10679 – kan May 5 '13 at 19:46
You might be interested in the dirtree and the forest package. Also take a look at Drawing a directory listing a la the tree command in TikZ and TeXample.net/filesystem tree. – Qrrbrbirlbel May 5 '13 at 19:51
I don't think we understand the question the same, but it may just be me not knowing how to change the style of the suggestions made so far. I'll add a picture to clarify. – LJNielsenDk May 5 '13 at 20:42
up vote 4 down vote accepted

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


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


  \TBox[fill=brown]{3cm}{\TBox[fill=red!30]{1cm}{B}\hfill\TBox[fill=cyan!30]{1cm}{C}} \\[1ex]


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):


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


  \TBox[fill=brown]{Some test text \\ \TBox[fill=red!30]{Some}\TBox[fill=cyan!30]{A}} \\


enter image description here

share|improve this answer
The spacing in the picture from the question is arbitrary and not something I actually want, it should just be some fixed small amount between each box. – LJNielsenDk May 5 '13 at 23:29
I'd like it automatically arrange figure out the size needed for each box based on it's contents because it will quickly become tiresome to have to compile the document and check all the time while building the diagram. – LJNielsenDk May 5 '13 at 23:31
@EbiDK Please see my updated answer; is it something like that what you want? – Gonzalo Medina May 5 '13 at 23:52
This is better, yes. – LJNielsenDk May 6 '13 at 0:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.