4

I read this question but I didn't get how to repeat the process more than one time. I make an example: I'd like to have a big green box which contains two copies of the light blue box presented in the other question. It, again, contains the blue boxes with the numbers.

I'm starting with this

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

Easy job with matrix library:

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

enter image description here

If you want to increase the sepration between two blue boxes use this:

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

enter image description here

Change xshift suitably.

  • Ok, next step. Add one more box level. How to add a red box surrounding the blue ones each? That's my problem. The use of pgfonlayer to create the bigger box. If I avoid that and I understand how to nest N levels I can do my job easily, even for the future! Thanks however for the current reply. :) – usernumberone Sep 24 '14 at 9:13
  • @usernumberone If you reveal a final picture of what you intend to have, it will be useful (instead of just saying n-levels) Is it like the one as posted by Ignasi? You are asking x but want to do y ;-) – user11232 Sep 24 '14 at 10:08
2

As Harish explained, a matrix offers two layers (inside nodes and matrix) but both are drawn on main (default) layer. backgrounds library introduces two more layers background and foreground. But if you need more layers you can define them with \pgfdeclarelayer and once declared order them with \pgfsetlayers command.

Next code based on Harish example shows how to use both commands.

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

enter image description here

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.