如何分割矩形?

如何分割矩形?

我希望有人能帮助我。我正在寻找像这样的矩形分割:

\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)$) {};

但是如果我想创建一个带有 x 变量的矩形,我该怎么做?

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

我想使用“for each”循环并通过北、南等访问我的 rec。我很感激任何帮助。谢谢!;-)

答案1

fit使用和TikZ 库positioning的解决方案chains

\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}

在此处输入图片描述

相关内容