N 级嵌套节点

N 级嵌套节点

我读过这个问题但我不知道如何重复这个过程多次。我举个例子:我想要一个大绿色盒子,里面有另一个问题中提到的浅蓝色盒子的两个副本。它同样包含带有数字的蓝色盒子。

我从这个开始

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit}

\begin{document}

\begin{tikzpicture}[outer sep=0.05cm,node distance=0.8cm,]
\tikzstyle{bigbox} = [draw=blue!50, thick, fill=blue!10, rounded corners, rectangle]
\tikzstyle{box} = [minimum size=0.6cm, rounded corners,rectangle, fill=blue!50]
%
\node[box] (11) {1};
\node[box,right of=11] (12) {2};
\node[box,right of=12] (13) {3};
\node[box,below of=11] (21) {4};
\node[box,right of=21] (22) {5};
\node[box,right of=22] (23) {6};
%
\begin{pgfonlayer}{background}
  \node[bigbox] [fit = (11) (23)] {};
\end{pgfonlayer}
%
\end{tikzpicture}

\end{document}

答案1

图书馆的轻松工作matrix

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,fit,matrix}
\tikzset{
   bigbox/.style = {draw=blue!50, thick, fill=blue!10, rounded corners, rectangle},
   box/.style = {minimum size=0.6cm, rounded corners,rectangle, fill=blue!50},
}


\begin{document}

\begin{tikzpicture}[outer sep=0.05cm,node distance=0.8cm]
   \matrix (a)[row sep=2mm, column sep=2mm, inner sep=2mm, bigbox, matrix of nodes, every node/.style=box]
      {
        1 & 2 & 3\\
        4 & 5 & 6\\
        };
\matrix (b)[row sep=2mm, column sep=2mm, inner sep=2mm, bigbox, matrix of nodes,
        every node/.style=box,anchor=west] at (a.east) {
            1 & 2 & 3\\
            4 & 5 & 6\\
        };
%
    \begin{pgfonlayer}{background}
        \node[bigbox,draw=green,fill=green!10]  [fit = (a) (b)] (A)  {};
    \end{pgfonlayer}
%
\end{tikzpicture}

\end{document}

在此处输入图片描述

如果你想增加两个蓝色框之间的分离,请使用以下命令:

\matrix (b)[row sep=2mm, column sep=2mm, inner sep=2mm, bigbox, matrix of nodes,
        every node/.style=box] at ([xshift=2cm]a.east) {
            1 & 2 & 3\\
            4 & 5 & 6\\
        };

在此处输入图片描述

适当改变xshift

答案2

正如 Harish 所解释的,matrix提供了两个层(内部节点和矩阵),但两者都绘制在main(默认)层上。backgrounds库引入了另外两个层backgroundforeground。但如果您需要更多层,您可以使用定义它们\pgfdeclarelayer,并且在声明后使用\pgfsetlayers命令对它们进行排序。

下一个基于 Harish 示例的代码展示了如何使用这两个命令。

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{fit,matrix}
\tikzset{
   bigbox/.style = {draw=blue!50, thick, fill=blue!10, rounded corners, rectangle},
   box/.style = {minimum size=0.6cm, rounded corners,rectangle, fill=blue!50},
}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfdeclarelayer{minusone}
\pgfdeclarelayer{minustwo}
\pgfdeclarelayer{minusthree}
\pgfsetlayers{minusthree,minustwo,minusone,background,main,foreground}

\begin{document}

\begin{tikzpicture}[outer sep=0.05cm,node distance=0.8cm]
   \matrix (a)[row sep=2mm, column sep=2mm, inner sep=2mm, bigbox, matrix of nodes, every node/.style=box]
      {
        1 & 2 & 3\\
        4 & 5 & 6\\
        };
\matrix (b)[row sep=2mm, column sep=2mm, inner sep=2mm, bigbox, matrix of nodes,
        every node/.style=box,anchor=west] at ([xshift=2cm]a.east) {
            1 & 2 & 3\\
            4 & 5 & 6\\
        };
%

    \begin{pgfonlayer}{background}
        \node[bigbox,draw=red,fill=red!10]  [fit = (a)] (A)  {};
        \node[bigbox,draw=red,fill=red!10]  [fit = (b)] (B)  {};
    \end{pgfonlayer}

        \begin{pgfonlayer}{minusone}
        \node[bigbox,draw=green,fill=green!10]  [fit = (A) (B)] (AB)  {};
    \end{pgfonlayer}
%

        \begin{pgfonlayer}{minustwo}
        \node[bigbox,draw=purple,fill=purple!10]  [fit = (AB)] (AB-1)  {};
    \end{pgfonlayer}

        \begin{pgfonlayer}{foreground}
        \node[bigbox,draw=orange,fill=orange!10,opacity=.5]  [fit = (a-2-2.west|-AB-1.south) (b-1-2.east|-AB-1.north)] (AB-2)  {};
    \end{pgfonlayer}

        \begin{pgfonlayer}{minusthree}
        \node[bigbox,draw=brown,fill=brown!10]  [fit = (AB-2) (AB-1)] (AB-3)  {};
    \end{pgfonlayer}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容