TikZ-PGF Nodes – How to Make a Rectangle Fill Up Space Given the Position of Neighboring Rectangles?

nodestikz-pgf

I am trying to create a figure consisting two stacks of rectangles, side by side. The right stack contains three rectangles, the left one two. The height of the rectangles depends on the text contents. The text contents in both stacks are the same, but given that the text is split over three rectangles in the right stack and that the rectangles are separated by some spacing, this stack is a bit higher.

I prefer to let TikZ figure out the sizes and positions of the rectangles. My strategy is to start with creating the boxes for the right stack and finish with the left stack. My question is how to get the top rectangle of the left stack to take up exactly as much space as to make the top of the stacks align.

I have tried the following, and the result is OK. I don't like the inner sep=0pt part though, which seems a hack and makes the upper lines of text in both stack not align horizontally. Also, the upper left rectangle seems a bit off.

Does anybody know of a way to insert the upper left rectangle such that it fits perfectly and the upper lines of text in both stacks align horizontally? Or is there maybe a better way to achieve the result I want?

Edit: Note that not each line of text in each stack needs to be horizontally aligned. I would like the first line of both upper rectangles to align horizontally. Subsequent lines within a box can just use the default line spacing. So item 7 (and item 1) should ideally be horizontally aligned.

Thanks in advance!
Kor

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit, positioning}


\pgfmathsetmacro \spacing {{0.07}}

\begin{document}
    \begin{tikzpicture}[
            level/.style={draw=black, text=black, align=center, minimum width=8.5em},
            bottom level/.style={level, fill=red},
            middle level/.style={level, fill=green},
            top level/.style={level, fill=blue},
        ]

        % Right, higher stack
        \node (r1) [bottom level] {item 1};
        \node (r2) [middle level, above=\spacing of r1]
            {item 6\\item 5\\item 4\\item 3\\item 2};
        \node (r3) [top level, above=\spacing of r2] {item 7};

        % Left stack, whose top should align with right stack
        \node (l1) [bottom level, left=(3 * \spacing) of r1] {item 1};
        \node (l2) [top level,
                    fit=(l1.west |- r2.south) (l1.east |- r3.north),
                    inner sep=0pt,
                ]
            {item 7\\item 6\\item 5\\item 4\\item 3\\item 2};
    \end{tikzpicture}
\end{document}

enter image description here

Best Answer

I'm not sure to understand the question but if the intention is to adjust boxes, a tcbposter can be an alternative. I don't know how OP want to compensate differences between vertical spaces due to different number of boxes, so this is just another alternative to star to work with.

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


\begin{document}
\begin{tcbposter}[
    poster={spacing=2mm, columns=2, height=13cm},
    boxes={sharp corners, notitle},
]
\posterbox{name=L1}{This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated.}


\posterbox[colback=red!30]{name=Lbottom, above=bottom}{This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated.}
\posterbox[colback=blue!20]{name=L2, between=L1 and Lbottom}{This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated.}

\posterbox[colback=green]{name=R1, column=2}{This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated.}

\posterbox[colback=purple!30]{name=R2, column=2, below=R1}{This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated.}

\posterbox[colback=blue!20]{name=Rbottom, column=2, above=bottom}{This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated. This is some text that will be repeated.}

\posterbox[colback=purple!30]{name=R2, column=2, between=R2 and Rbottom}{This is some text that will be repeated. This is some text that will be repeated.}
\end{tcbposter}
\end{document}

enter image description here