6

My goal is to get a rectangle like the one below, but with text in the nested rectangles.

enter image description here

Line types could be for example the following: enter image description here

I saw tikz: Multiple splits of rectangle, but it does not answer the question on the line types. Beyond that, I am a beginner using Tikz, and I'd like the answer explained, since the linked answer is complex to understand.

Thanks!

4
  • Could you show an example sketch of "with text in the nested rectangles"? Commented 2 days ago
  • 2
    I updated the plot with text Commented 2 days ago
  • I agree with samcarter that table might be a better practice, and maybe you could refer: tikz.dev/library-shapes#pgf.rectangle:split Commented 2 days ago
  • Is box2 supposed to be right aligned or just half width? Commented yesterday

4 Answers 4

6

I'd suggest to use a table instead. The tabularray package even has a tikz library, so if you need additional tikz elements, you could add them at will.

\documentclass{article}

\usepackage{tabularray}
\NewTblrDashStyle{longdashed}{on 4pt off 2pt}

\begin{document}

\begin{tblr}{
  colspec={X},
  vlines,hlines,
  row{2}={halign=r},
  row{3}={halign=c},
  hline{2}={dashed},
  hline{3}={longdashed},
  hline{4}={dotted}
}
{box 1 says hello\\also in second line}\\
{box 2 does not say\\ anything}\\
{probably I would prefer\\ all text centred}\\
{Or maybe I'd prefer\\ all left aligned?}\\
\end{tblr}

\end{document}

enter image description here

6

You could try the following option. This avoids the more complicated rectangle split machinery and gives direct control over every line type.

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

\begin{document}

\begin{tikzpicture}[
    thickline/.style={line width=1.2pt},
    longdash/.style={dash pattern=on 12pt off 7pt},
    every node/.style={font=\sffamily\small}
]

% Size of the big rectangle
\def\W{12}
\def\H{8}

% y-coordinates of the horizontal dividing lines
\def\yA{6.0}
\def\yB{4.1}
\def\yC{2.2}

% Outer rectangle
\draw[thickline] (0,0) rectangle (\W,\H);

% Internal horizontal lines
\draw[thickline,longdash] (0,\yA) -- (\W,\yA);
\draw[thickline]          (0,\yB) -- (\W,\yB);
\draw[thickline,dotted]   (0,\yC) -- (\W,\yC);

% Text
\node[anchor=north west, align=left] at (1.4,7.4)
  {box1 says hello\\also in second line};

\node[anchor=north west, align=left] at (6.1,5.5)
  {box2 does not say\\anything};

\node[align=center] at (6,3.25)
  {probably I would prefer\\all text centered};

\node[anchor=west, align=left] at (0.9,1.1)
  {Or maybe I'd prefer\\all left aligned?};

\end{tikzpicture}

\end{document}

enter image description here

4

Here below is my proposal based on my comment with shapes.multipart:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}
    \node[
        align=left,draw,ultra thick,
        minimum width=8cm,
        text width=6cm,
        rectangle split,
        rectangle split parts=4, 
        rectangle split draw splits={false}
    ] (tmp)
    {%
        box 1 says hello\\also in second line
        \nodepart[align=right]{two}
           box 2 does not say\\ 
           anything
        \nodepart[align=center]{three} 
            probably I would prefer\\ 
            all text centred
        \nodepart[align=left]{four} 
            Or maybe I'd prefer\\ 
            all left aligned?
    }; 
    \draw[dashed,thick] (tmp.text split west)--(tmp.text split east);
    \draw[longdashed,thick] (tmp.two split west)--(tmp.two split east);
    \draw[dotted,thick] (tmp.three split west)--(tmp.three split east);
\end{tikzpicture}

\end{document}

res

If you stick to use tikz and want to regards them as a whole, that is an option.

2

With a tabular {NiceTabular} of nicematrix and horizontal rules drawn by TikZ.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{decorations}

\begin{document}

\begin{NiceTabular}{|X[l]X[c]X[r]|}[cell-space-limits=2pt]
\Hline
box 1 says hello also in second line \\
\hdashedline
& & box 2 does not say anything \\
\Hline[tikz=dotted]
\Block{1-3}{probably I would prefer all text centred}  \\
\Hline[tikz = loosely dotted]
Or maybe I'd prefer all left aligned? \\
\Hline
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.