2

I would like to draw something like this with Tikz (exemple given with 4 boxes but it could have more boxes):

enter image description here

For the moment, I have obtained this (thanks to this answer and this one (for the pgfonlayer):

enter image description here

With this code:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{backgrounds, 
    fit,
    positioning,
    shapes.multipart}

\pgfdeclarelayer{layer1} \pgfdeclarelayer{layer2} \pgfdeclarelayer{layer3} \pgfsetlayers{main,layer1,layer2,layer3}

\definecolor{CoreBlue}{HTML}{5b9bd5} \definecolor{CoreOrange}{HTML}{ec7d2d} \begin{document} \tikzset{ base/.style = {text width=#1, align=center, text=white, outer sep=0pt}, base/.default = 17mm, CB/.style = {base=#1, fill=CoreBlue}, CO/.style = {base=#1, fill=CoreOrange}, } \begin{tikzpicture}[] \begin{pgfonlayer}{layer2} \node (1a) [CB] {Box 1}; \node (1b) [CO,below=0mm of 1a] {Box 2}; \

    \end{pgfonlayer}
    \begin{pgfonlayer}{layer1}
            \node (1)  [CB, fit=(1a) (1b)] {};
    \end{pgfonlayer}
    \begin{pgfonlayer}{layer3}
        \node (1c) [CB,below=0mm of 1b] {Box 3}; \\
    \end{pgfonlayer}

    \end{tikzpicture}

\end{document}

zetyty
  • 779
  • What's your end goal here? Why is 1c below 1b and not behind it? If it's just one box with boxes around it, you could draw a thick line around the smaller box (or inverse clip against them) or … – Qrrbrbirlbel Aug 31 '22 at 15:21
  • My MWE can mislead because I don't master Tikz at all... My goal is to get the first figure and with rectangles positioned relatively to the previous or next one preferably. – zetyty Sep 01 '22 at 08:06

4 Answers4

6

You don't need TikZ, because there are only colored \vrules here.

In the following example I use colors settings used in OpTeX. You can replace it by colors setting used in your macro, if needed. But the main idea is that there are only nested \vrules.

\newdimen\nboxw  \nboxw=\hsize
\def\nestbox#1#2#3{%
   \hbox{#2\vrule width\nboxw height1em \kern-\nboxw \Black \rlap{\ #1}\kern1.4em
         \vtop{\kern.4em \advance\nboxw by-2.8em \hbox{#3}\kern1.4em}}%
}

\nestbox{Box 1}{\Red}{% \nestbox{Box 2}{\Brown}{% \nestbox{Another box}{\Yellow}{% \nestbox{One more box}{\Green}{% \nestbox{Last box}{\Blue}{\vbox to3em{}}% }}}}

\bye

The result is the same as in Sandy answer.

wipet
  • 74,238
  • Thank you for this solution. I got an error when try to compile it with LuaLaTeX... I put the \begin{document} just before \nestbox{Box 1}{\Red}{% and I used the article documentclass. – zetyty Sep 01 '22 at 08:21
  • 1
    @SylvainRigal I have noticed that the colors selectors here are from OpTeX (no LaTeX). You can try my code with OpTeX directly and it will wok. Or (if you insist to LaTeX) you have to modify the code (insert some commands before and after it and replace color selectors \Red, \Black etc.) to color selectors used by a LaTeX package designed for colors. I don't do this because I am not using LaTeX. – wipet Sep 01 '22 at 08:31
4

Like this?

enter image description here

The code:

\documentclass[tikz, border=2mm]{standalone}
\definecolor{CoreBlue}{HTML}{5b9bd5}
\definecolor{CoreOrange}{HTML}{ec7d2d}
\begin{document}
\begin{tikzpicture}[]
    \filldraw[CoreBlue] (0,0) rectangle (10,10);
    \node at (.5,9.5) () {Box1};
    \filldraw[CoreOrange] (1,1) rectangle (9,9);
    \node at (1.5,8.5) () {Box2};   
    \filldraw[CoreBlue] (2,2) rectangle (8,8);
    \node at (2.5,7.5) () {Box3};
    \filldraw[CoreOrange] (3,3) rectangle (7,7);
    \node at (3.5,6.5) () {Box4};           
\end{tikzpicture}

\end{document}

EDIT: Other geometry enter image description here

Code:

\documentclass[tikz, border=2mm]{standalone}
\definecolor{CoreBlue}{HTML}{5b9bd5}
\definecolor{CoreOrange}{HTML}{ec7d2d}
\begin{document}
\begin{tikzpicture}[]
    \filldraw[CoreBlue] (0,0) rectangle (10,7);
    \node at (.5,6.75) () {Box1};
    \filldraw[CoreOrange] (.5,.5) rectangle (9.5,6.5);
    \node at (1,6.25) () {Box2};    
    \filldraw[CoreBlue] (1,1) rectangle (9,6);
    \node at (1.5,5.75) () {Box3};
    \filldraw[CoreOrange] (1.5,1.5) rectangle (8.5,5.5);
    \node at (2,5.25) () {Box4};    

\end{tikzpicture}

\end{document}

  • It does the job thanks but I would prefer a solution where the rectangles are positioned relatively to the previous or next one and not with absolute coordinates. This was not clearly stated in my question, I'm sorry for that. – zetyty Sep 01 '22 at 08:10
4

Here is a macro \nestboxes to customize the effect. The syntax is

\nestboxes[<tikz attributes>]{<width in cm>}{height in cm}{<list of colors and labels>}.

<tikz attributes> is optional but can include things like xscale= or rotate=30, transform shape. Width and height are required. Each rectangle is 1cm smaller in each dimension than the previous. Each element in the <list of colors and labels> should have the form <label>/<color>.

enter image description here

Here is the code:

\documentclass{article}

\usepackage{tikz}

\newcommand{\nestboxes}[4][]{ \begin{tikzpicture}[#1] \foreach \L/\C[count=\n from 0] in {#4}{\fill[\C] (\n/2,#3-\n/2)node[black, below right]{\L} rectangle (#2-\n/2,\n/2);} \end{tikzpicture} }

\begin{document}

\nestboxes{10}{6}{Box 1/red, Box 2/orange, Another box/yellow, One more box/green, Last box/blue!75}

\end{document}

Sandy G
  • 42,558
  • Perfect solution for me ! Thanks a lot. For your information, I tried the rotate option but the text doesn't rotate, just the colored rectangles do (I don't need this fonctionality for the moment so I don't ask you to spend time on it, it's just in order to leave something perfectly functional to future visitors ;). – zetyty Sep 01 '22 at 08:27
2

With tcolorbox you can obtain similar results:

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcolorbox{mybox}[2][]{sharp corners, colback=#2, colframe=#2, #1}

\begin{document}

\begin{mybox}{cyan!90!black} Box 1 \begin{mybox}{orange!75!red} Box 2 \begin{mybox}{cyan!90!black} Box 3 \begin{mybox}[height=2cm]{orange!70!red} Box 5 \end{mybox} \end{mybox} \end{mybox} \end{mybox} \end{document}

enter image description here

Ignasi
  • 136,588