0

I am try to create a diagram similar to the following in Tikz:

enter image description here

I know how to create each individual row I think, I'm doing something similar to:

\tikzstyle{cell} = [rectangle, rounded corners=5pt, thick, draw, text width=0.65cm ]
\node [cell, text width=8.7cm] (n1) {Z};

I figured I could stack them with something like:

\node [cell, text width=8.7cm] (n1) [above=of n2]{Z};                                                                                 
\node [cell, text width=4.1cm] (n2) {X};                                                                                              
\node [cell, text width=4.1cm] (n3) [right=of n2] {Y};

but that doesn't stack them the way I want to. I've seen similar answers using stuff like "above right", etc. but those seem to place them diagonally instead of aligned like the picture above.

Any help would be appreciated - thanks!

  • 1
    Welcome to TeX.SX! Please make your code complete adding \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – CarLaTeX Sep 10 '17 at 4:49
5

I think that a tikz matrix and the fit library could help you.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fit, matrix, shapes.geometric}
\tikzset{% use tikzset, not tikzstyle
    cell/.style={
        rectangle, rounded corners=5pt, thick, draw,
    }
}
\begin{document}
    \begin{tikzpicture}
        \matrix[
            matrix of nodes, nodes in empty cells,
            minimum width=2cm,
            column sep=4pt,
            row sep=10pt,
            text height=2ex,text depth=.25ex,
            minimum height=4ex
            ] (m) {
            & & & \\
            & & & \\
            |[cell]| A & |[cell]| B & |[cell]| C & |[cell]| D \\
        } ;
        \node[cell, fit=(m-2-1)(m-2-2), inner sep=0pt, label=center: X] {};
        \node[cell, fit=(m-2-3)(m-2-4), inner sep=0pt, label=center: Y] {};
        \node[cell, fit=(m-1-1)(m-1-4), inner sep=0pt, label=center: Z] {};        
    \end{tikzpicture}
\end{document}

enter image description here

Credits for the label=center: goes to Why is the text of this node not vertically centered?.

4

This is a variation from CarLaTeX solution. It uses a matrix for the bottom row, but upper rows are built with a fit node placed with positioning library.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{fit, matrix, positioning, shapes.geometric}
\tikzset{% use tikzset, not tikzstyle
    cell/.style={
        minimum width=2cm,
        text height=2ex,text depth=.25ex,
        minimum height=4ex,
        rectangle, rounded corners=5pt, thick, draw,
    }
}
\begin{document}
    \begin{tikzpicture}
        \matrix[
            matrix of nodes,
            nodes = {cell, align=center},
            column sep=4pt,
            ] (m) {
            A & B & C & D \\
        } ;
        \node[cell, fit=(m-1-1)(m-1-2), inner sep=0pt, label=center: X, above left = 10pt and 0pt of m-1-2.north east] (X) {};
        \node[cell, fit=(m-1-3)(m-1-4), inner sep=0pt, label=center: Y, above left = 10pt and 0pt of m-1-4.north east] (Y) {};
        \node[cell, fit=(X)(Y), inner sep=0pt, label=center: Z, above left = 10pt and 0pt of Y.north east] {};        
    \end{tikzpicture}
\end{document}

enter image description here

Following solution uses a tcbraster from tcolorbox package.

\documentclass{article}

\usepackage[most]{tcolorbox}

\tcbset{enhanced, fontupper=\bfseries, notitle, halign=center, valign=center}

\begin{document}
\begin{tcbitemize}[%
    raster width=10cm,
    raster equal height=rows, 
    raster columns=4,
    raster equal height,
    raster every box/.style={height=4ex},
    raster column skip=10pt,
    raster row skip=4pt,
    colback=white]
\tcbitem[raster multicolumn=4] Z
\tcbitem[raster multicolumn=2] X
\tcbitem[raster multicolumn=2] Y
\tcbitem A
\tcbitem B
\tcbitem C
\tcbitem D
\end{tcbitemize}
\end{document}

enter image description here

  • Oo interesting, I did not know about the raster package, thanks! – adelbertc Sep 10 '17 at 23:22
2

With nested loops, no TikZ matrix, nor any TikZ library.

I use the nice label=center:#1 trick suggested by Carla.

It is certainly possible to manage both node length and coordinates xscale better, but I don't know how off the top of my head.

The output

enter image description here

The code

\documentclass[12pt,tikz]{standalone}
\begin{document}
\tikzset
{
  cell/.style=
  {
    rectangle,
    rounded corners=5pt,
    thick,
    draw,
    anchor=west,
    inner xsep=-2pt,
    minimum height=.7cm,
  }
}
\def\xScale{7cm}
\begin{tikzpicture}[x=\xScale]
  \foreach \k/\v in {0/{0/Z},1/{0/X,1/Y},2/{0/A,1/B,2/C,3/D}}
  {
    \foreach \kk/\vv in \v
    {
        \node 
        [
          cell,
          text width=1/2^\k * \xScale,
          label=center:\vv,
        ]
        at (\kk/2^\k,-\k)
        {};
    }
  }
\end{tikzpicture}
\end{document}
  • Ah interesting use of for loops, speaks to the programmer side of me :-) Thank you! – adelbertc Sep 10 '17 at 23:22
2

as forest tree ... (adopted example from its documentation, page 29):

\documentclass[margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
\forestset{box/.style={
  draw, rounded corners, minimum width=12mm,
  no edge,
  l=0, l sep=1mm,
  s=0, s sep=1mm,
  calign=first, anchor=base west,
  % content format={\strut\forestoption{content}},
  if n children=0{}{
    after packing node={
      minimum width/.pgfmath=
        {s("!l")+max_x("!l")-s("!1")-min_x("!1")},
      for children/.wrap pgfmath arg={s+={##1}}{0},
      typeset node}}}}
\begin{forest} for tree={box}
[Z
  [X
    [A][B]
  ]
  [Y
    [C]
    [D]
  ]
]
\end{forest}
\end{document}

enter image description here

  • That's pretty interesting, the way you can lay out the nodes in a quasi-WYSIWYG style is neat. Thanks! – adelbertc Sep 10 '17 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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