I built up a simple UML diagram, where one of the class boxes has an union containing a number of elements. I want to show that those elements are part of an union.

But how can I do so with tikz? I tried to use a matrix, but it complains that I cannot nest matrices.



\tikzstyle{class}=[matrix of nodes, draw=black, font=\ttfamily\small]


\begin{tikzpicture}[node distance=2cm]
    \matrix (value) [class] {
        \textbf{Value} \\
        TypeA m\_type \\
        \textit{Union:} \\ % TODO: want to have a rectangle around the next 6 entries!
        TypeB int64Val\\
        TypeC float64Val\\
        TypeD stringVal\\
        TypeE *funcVal\\
        TypeF varsVal\\

  • 1
    Please always provide an example that can be copy&pasted to compile (so it should in particular include all \tikzlibrary statements and not reference any nodes or styles not defined in the example).
    – Caramdir
    Feb 10, 2011 at 16:24
  • @Caramdir done :) Feb 10, 2011 at 16:41

1 Answer 1



\draw (value.north west) rectangle (value.south east);
\draw ($(value.north west)!(value-3-1.north west)!(value.south west) + (4pt,0)$) rectangle ($(value.south east) + (-4pt,4pt)$);

The first coordinate in the draw statement is the projection of (value-3-1.north west) (top left anchor the the first node in the third row) to the line between (value.north west) and (value.south west) and then shifting 4pt to the right.


  • Ohh great! I'm sorry for not mentioning that I have a frame around the "class" box too. I would like to have both the class frame and the union frame have some little space. Can I do that easily? Feb 10, 2011 at 16:29
  • Hmm, maybe there is some "scale" utility or something, that scales the rectangle around its center? Ideally I would like to tell the "draw" command: "Draw the rectangle X pt smaller than the coordinates given". Feb 10, 2011 at 16:35
  • @Johannes: See the updated answer
    – Caramdir
    Feb 10, 2011 at 16:36
  • 1
    @Martin: That wouldn't just have pushed the inner box outward too in my original solution.
    – Caramdir
    Feb 10, 2011 at 16:39
  • 1
    @Martin: I TikZ padding (=inner sep) is part of the width, in CSS I think it isn't (except in old IE versions).
    – Caramdir
    Feb 10, 2011 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .