Tikz:嵌套框对齐

Tikz:嵌套框对齐

我正在尝试制作一个看起来像这样的图形(不同大小和对齐方式的框行,里面有文字)

在此处输入图片描述

使用 Tikz。我花了一些时间研究不同的方法,主要是以下方法先前的答案

\matrix我曾尝试过几次失败的方法,分别使用了、\coordinate和的方法chains。如下所示。

实现这一目标的适当方法是什么?

矩阵法 在此处输入图片描述

\documentclass[border=10pt,multi,tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\tikzstyle{bigbox} = [draw=black, thick, rectangle]
\tikzstyle{box} = [draw=black,minimum size=0.6cm, rectangle]
\tikzstyle{box1} = [draw=black,minimum size=0.6cm, rectangle, minimum width=2cm]
\tikzstyle{box2} = [draw=black,minimum size=0.6cm, rectangle, minimum width=5cm]
%
\matrix[row sep=2mm, column sep=2mm, inner sep=2mm, bigbox, every node/.style=box] {
\node[minimum width=15cm] {1}; \\
\node[style=box1] {2}; & \node[style=box1] {3};  & \node[style=box1] {4};  & \node[style=box1] {5};\\
\node[style=box1] {5}; & \node[style=box1] {6};  & \node[style=box1] {7};  & \node[style=box1] {8};\\
\node[style=box2] {9}; & \node[style=box2] {10};\\
\node[style=box2] {11}; & \node[style=box2] {12};\\
\node[style=box2] {13}; & \node[style=box2] {14};\\
};
%
\end{tikzpicture}
\end{document}

非常糟糕的坐标方法 在此处输入图片描述

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{arrows.meta,positioning}
\tikzset{
    box/.style={rectangle, text centered, minimum height=3em,text width=130mm,draw},
    narrowbox/.style={box,text width=4cm,draw,thick},
    line/.style={draw, thick, -Stealth}
}
\begin{document}
\begin{tikzpicture}[auto]
  \node [box]                                     (1)      {1};
  \coordinate [below=0.5cm of 1]              (coord1);
  % \coordinate [below=0.5cm of 1]              (2) {2};
  \node [narrowbox, below=0.5cm of coord1]        (2)    {2};
  \node [narrowbox, anchor=west]          (1) at (2 -| 1.west)    {3};
  \node [narrowbox, anchor=east]         (3) at (2 -| 1.east)    {4};
  \node [narrowbox, anchor=east]         (4) at (3 -| 1.east)    {5};
  \node [narrowbox, below=0.5cm of 3]         (select1)  {s1};
  \node [narrowbox, below=0.5cm of 1]         (select)   {s};
  \node [box, text width={(3/7)*130mm}, below=4.5cm of 1.south west, anchor=north west]               (decide)   {Shorter};
  \node [box, text width={(3/7)*130mm}, below=4.5cm of 1.south east, anchor=north east]               (11)   {12};
  \node [box, below=0.5cm of decide.south west, anchor=north west]              (inter)    {it};


\end{tikzpicture}
\end{document}

链式方法 在此处输入图片描述

\documentclass[border=2px]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, chains, calc}

\tikzset{
    box/.style={rectangle, text centered, minimum height=3em,text width=130mm,draw},
    narrowbox/.style={box,text width=4cm,draw,thick},
    line/.style={draw, thick, -Stealth}
 }

\begin{document}
\begin{tikzpicture}[start chain=1 going right,
  start chain=2 going right,
  start chain=3 going right,
  start chain=4 going right,
  start chain=5 going right,
  start chain=6 going right,
  node distance=1mm]

  \node [narrowbox,name=r1c1, on chain=1] {1};
  \node [narrowbox,name=r2c1, on chain=2] {2};
  \node [narrowbox,name=r2c2, on chain=2] {3};
  \node [narrowbox,name=r2c3, on chain=2] {4};
  \node [narrowbox,name=r2c4, on chain=2] {5};
  \node [narrowbox,name=r3c1, on chain=3] {6};
  \node [narrowbox,name=r3c2, on chain=3] {7};
  \node [narrowbox,name=r3c3, on chain=3] {8};
  \node [narrowbox,name=r3c4, on chain=3] {9};
  \node [narrowbox,name=r4c1, on chain=4] {10};
  \node [narrowbox,name=r4c2, on chain=4] {11};
  \node [narrowbox,name=r5c1, on chain=5] {12};
  \node [narrowbox,name=r5c2, on chain=5] {13};
  \node [narrowbox,name=r6c1, on chain=6] {14};
  \node [narrowbox,name=r6c2, on chain=6] {15};
  % \draw let \p1=($(r1c1.west)$), \n1 = {veclen(\x1,\y1)} in
  % node [name=r2c1, on chain=2, blue, anchor=north west, yshift=-1mm,
  % minimum width=\n1-\pgflinewidth]
  % at (r1c1.south west) {COM-API In};
  % \draw let \p1=($(r1c4.east)-(r1c1.west)$), \n1 = {veclen(\x1,\y1)} in
  % node [name=r3c1, on chain=2, yellow, minimum width=\n1-\pgflinewidth] {Autodesk Inventor Application};

\end{tikzpicture}
\end{document}

编辑 显然我对 TikZ 还不太熟悉。我最近发现有很多图形用户界面可以让你快速设计图形并导出到 LaTeX 代码。显然,这样做会失去很多功能,而且导出的代码通常难以辨认。但是,如果你像我一样是个新手,只想快速绘制图形,那么它可能值得考虑。我一直在使用迪亚

答案1

positioning许多可能的解决方案之一,使用、chains和库的组合fit

在此处输入图片描述

\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{positioning,chains,fit}
\begin{document}
\begin{tikzpicture}[
 declare function={
   boxunit=1cm;
   boxsep=0.2cm;
   bigboxW=boxunit*4 + 3*boxsep;
   midboxW=boxunit*2 + boxsep;
 },
 box/.style={draw,minimum width=#1},
 box/.default={boxunit}
]

\node [box=bigboxW,name=1] {1};

\begin{scope}[
  start chain=r1,
  node distance=boxsep-\pgflinewidth,
  every node/.style={box=boxunit, on chain}
]

\node [below=boxsep-\pgflinewidth of 1.south west,anchor=north west] {2};
\node {3};
\node {4};
\node {5};
\end{scope}

\begin{scope}[
  start chain=r2,
  node distance=boxsep-\pgflinewidth,
  every node/.style={box=boxunit, on chain}
]

\node [below=boxsep of r1-1.south west,anchor=north west] {6};
\node {7};
\node {8};
\node {9};
\end{scope}

\begin{scope}[
  start chain=c1 going below,
  node distance=boxsep-\pgflinewidth,
  every node/.style={box=midboxW, on chain}
]

\node [below=boxsep of r2-1.south west,anchor=north west] {10};
\node {12};
\node {14};
\end{scope}

\begin{scope}[
  start chain=c2 going below,
  node distance=boxsep-\pgflinewidth,
  every node/.style={box=midboxW, on chain}
]

\node [below=boxsep of r2-3.south west,anchor=north west] {11};
\node {13};
\node {15};
\end{scope}

\node [inner sep=boxsep,fit=(1)(c2-3),draw] {};

\end{tikzpicture}
\end{document}

相关内容