1

The following code:

\documentclass[tikz, border=1mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{backgrounds, positioning, fit}
\begin{document}

\newcommand{\boxdist}{3cm}
\newcommand{\textwdh}{2cm}

\begin{tikzpicture}[
    box/.style={rectangle, rounded corners, draw=black, text width=15mm},
    ]
    \node (A) [text width=\textwdh] 
    {Blaaaaaa bbbbbbbb \newline Some text text text text};
    \node (B) [right= \boxdist of A.west, text width=\textwdh] {More text text text};
    \node (C) [right= \boxdist of B.west, text width=\textwdh] {Even more text test};
    \node (D) [right= \boxdist of C.west, text width=\textwdh] {And even more text};

    \begin{scope}[on background layer]
        \node [box, inner sep=11mm, fill=black!40, fit={(A) (B) (C) (D)}] {};
    \end{scope}

    \begin{scope}[on background layer]
        \node [box, inner sep=9mm, fill=black!30, fit={(B) (C) (D)}] {};
    \end{scope}

    \begin{scope}[on background layer]
        \node [box, inner sep=7mm, fill=black!20, fit={(C) (D)}] {};
    \end{scope}

    \begin{scope}[on background layer]
        \node [box, inner sep=5mm, fill=black!10, fit={(D)}] {};
    \end{scope}

\end{tikzpicture}

\end{document}

produces the following output:

enter image description here

I want the text in each box to start in the upper left corner. I also want the text to have the same width in all boxes. How do I do this?

2

Like this?

enter image description here

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
    \begin{tikzpicture}[
  node distance = 4pt and 4pt,
     box/.style = {rectangle, inner sep=0pt, text width=21mm},
FIT/.style args = {#1/#2}{rectangle, rounded corners, draw, semithick,
                    fill=gray!#1, inner sep=2pt, fit=#2,
                    node contents={}}
                        ]
\node (A) [box] {Blaaaaaa bbbbbbbb Some text text text text};
\node (B) [box, below right=of A.north east] {More text text text};
\node (C) [box, below right=of B.north east] {Even more text test};
\node (D) [box, below right=of C.north east] {And even more text\\~};
%
\begin{scope}[on background layer]
\node (d) [FIT=0/(D)];
\node (c) [FIT=0/(C) (d)];
\node (b) [FIT=0/(B) (c)];
\node (a) [FIT=0/(A) (b)];
%
\node [FIT=60/(a)];
\node [FIT=40/(b)];
\node [FIT=20/(c)];
\node [FIT=10/(d)];
\end{scope}
    \end{tikzpicture}
\end{document}

addendum: A case when the text in nodes are vertical is centered (as you request in comment):

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
    \begin{tikzpicture}[
  node distance = 4pt and 4pt,
     box/.style = {rectangle, inner sep=0pt, text width=21mm},
FIT/.style args = {#1/#2}{rectangle, rounded corners, draw, semithick,
                    fill=gray!#1, inner sep=2pt, fit=#2,
                    node contents={}}
                        ]
\node (A) [box] {Blaaaaaa bbbbbbbb Some text text text text};
\node (B) [box, right=of A] {More text text text};
\node (C) [box, right=of B] {Even more text test};
\node (D) [box, right=of C] {And even more text};
%
\begin{scope}[on background layer]
\node (d) [FIT=0/(D)];
\node (c) [FIT=0/(C) (d)];
\node (b) [FIT=0/(B) (c)];
\node (a) [FIT=0/(A) (b)];
%
\node [FIT=60/(a)];
\node [FIT=40/(b)];
\node [FIT=20/(c)];
\node [FIT=10/(d)];
\end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

6
  • Wow! What exactly happens in the line starting with FIT/.style args? Is that a function with two arguments?
    – Samuel
    Jan 17 '19 at 7:46
  • 1
    @Samuel, yes. it is. the first define percentage of gray color and second nodes/coordinates around each is node "FIT".
    – Zarko
    Jan 17 '19 at 8:30
  • Great! Is there a fast solution to center the text vertically?
    – Samuel
    Jan 17 '19 at 8:32
  • @Samuel, your mwe center text vertically ... do you like to have for it more cincise code as is in my answer? or do i not understand you?
    – Zarko
    Jan 17 '19 at 13:42
  • Yes, my MWE does center the text vertically but does not align the text on the left. Your answer is great since it aligns the text on the left side of the node. So I wanted to know, how I can align the text in your example not only on the left but also vertically.
    – Samuel
    Jan 17 '19 at 19:12

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.