6

I hope somebody can help me. I am search for a split of a rectangle like this:

\node[rectangle,draw,minimum width=3cm, minimum height=3cm] (rec) {};
\node[name=v1,minimum width=3cm * 0.16cm] at
        ($(rec.north west)!0.5!(rec.west)!0.18!(rec.center)$){$v_1$};
\node[name=v2, minimum width=3cm * 0.16cm] at
    ($(rec.south west)!0.5!(rec.west)!0.18!(rec.center)$){$v_2$};
\draw (rec.north west) rectangle ($(rec.west)!0.32!(rec.center)$) {};
\draw (rec.south west) rectangle ($(rec.west)!0.32!(rec.center)$) {};

But what can I do if I would like to create a rectangle with x variables?

4 variables
+---------+-------------+
|    v1   |             |
+---------+             |
|    d    |             |
+---------+             |
|    c2   |             |
+---------+             |
|    f    |             |
+---------+-------------+

I would like to use a 'for each'-loop and access my rec via north, south, etc. I appreciate any help. Thank you! ;-)

drh
  • 61

1 Answers1

7

A solution using fit, positioning and chains TikZ libraries:

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{fit,positioning,chains}
\begin{document}
\begin{tikzpicture}
  \tikzset{
    mybox/.style={
      draw,line width=1pt,align=center,
      text width=1cm,text height=.8em,text depth=.2em,
    },
  }
  \begin{scope}[start chain=going below,node distance=-1pt]
    \foreach \x in {a,v1,d,c2,f,g,h}{
      \node[on chain,mybox](\x){\x};
    }
  \end{scope}
  \coordinate (right) at ([xshift=2cm]a.east);

  \node[fit=(a)(h)(right),inner sep=-.5pt,line width=1pt,draw=blue]
  (rec){};
\end{tikzpicture}
\end{document}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283