2

I have recently started using tikz and discovered that you can both chain nodes and nest them using the chain and fit libraries. I have done both successfully:

Fit:

\begin{tikzpicture}[node distance = 12mm and 0mm, start chain=c1 going below]
    \node [draw=black!50] (content1) {
        Some text
    };
    \node [draw=black!50, below=0mm of content1.south] (content2) {
        Some text
    };
    \node [draw=black!50, fit={(content1) (content2)}] (box1) {};
\end{tikzpicture}

enter image description here

Chain:

\begin{tikzpicture}[node distance = 12mm and 0mm, start chain=c1 going below]
    \node [draw=black!50, on chain=c1] (content1) {
        Some text
    };
    \node [draw=black!50, on chain=c1] (content2) {
        Some text
    };
\end{tikzpicture}

enter image description here

However, when I combine them:

\begin{tikzpicture}[node distance = 12mm and 0mm, start chain=c1 going below]
    \node [draw=black!50] (content1) {
        Some text
    };
    \node [draw=black!50] (content2) {
        Some text
    };
    \node [draw=black!50, fit={(content1)}, on chain=c1] (box1) {};
    \node [draw=black!50, fit={(content2)}, on chain=c1] (box2) {};
\end{tikzpicture}

The chaining doesn't work on the nesting nodes:

enter image description here

I would like the chaining to work on a hierarchical level such that if the box nodes are chained, these will bring the content nodes with them. I have produced a hard coded example of how I want it to look here:

\begin{tikzpicture}[node distance = 12mm and 0mm, start chain=c1 going below]
    \node [draw=black!50] (content1) {
        Some text
    };
    \node [draw=black!50, below=of content1, yshift=-2.5mm] (content2) {
        Some text
    };
    \node [draw=black!50, fit={(content1)}] (box1) {};
    \node [draw=black!50, fit={(content2)}, below=of box1] (box2) {};
\end{tikzpicture}

enter image description here

My question is: Is this possible to achieve dynamically such that the content nodes aren't aware of each other?

2
  • 1
    Welcome to TeX.SX! I am unsure what you want to achieve. You should first place nodes on the chain using on chain and only then fit the other nodes to these nodes. So, \node [draw=black!50, on chain=c1] (content1) {Some text}; \node [draw=black!50, fit={(content1)}] (box1) {}; should work. Commented Sep 11, 2022 at 14:39
  • 1
    fit just measures and places nodes. It does nothing else with the given nodes, certainly no chaining. on chain also does placement (and a little bit more, depending on the chain and settings). You can use \chainin on an already placed node. Do you just want nodes on a chain with a double line? Commented Sep 11, 2022 at 15:59

2 Answers 2

2

Is this what you want to achieve? I think, you should first place nodes on the chain using on chain and then use fit to place other nodes onto the nodes placed on the chain:

\documentclass[border=10mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,fit}

\begin{document}

\begin{tikzpicture}[node distance=12mm and 0mm, start chain=c1 going below]
    \node [draw=black!50, on chain=c1] (content1) {
        Some text
    };
    \node [draw=black!50, on chain=c1] (content2) {
        Some text
    };
    \node [draw=black!50, fit={(content1)}] (box1) {};
    \node [draw=black!50, fit={(content2)}] (box2) {};
\end{tikzpicture}

\end{document}

enter image description here

Using a fit node, you can wrap as many other nodes as you want, it will automatically resize according to the size of the wrapped nodes:

\documentclass[border=10mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,fit}

\begin{document}

\begin{tikzpicture}[node distance=12mm and 0mm, start chain=c1 going below]
    \node [draw=black!50, on chain=c1] (content1) {
        Some text
    };
    \node [draw=black!50, on chain=c1] (content2) {
        Some text
    };
    \node [draw=black!50, on chain=c1, align=center] (content3) {
        Some longer text \\ over two lines
    };
    \node [draw=black!50, fit={(content1)}] (box1) {};
    \node [draw=black!50, fit={(content2) (content3)}] (box2) {};
\end{tikzpicture}

\end{document}

enter image description here

2
  • Thanks! I have to be honest, though, and say that the question I asked was a simplified version of what I want to achieve. Therefore, I wonder how I can achieve the same result if the fitted node contains multiple nested nodes above each other? I.e. that we would have 2+ Some text nodes within the fitted nodes. And that these would be of arbitrary height. Commented Sep 11, 2022 at 18:31
  • @AndreasForslöw I am still unsure how the final result should look like, but maybe the additional example above can help you ... Commented Sep 12, 2022 at 11:40
1

A fit node is just a node whose dimensions are computed according other nodes. And a chain is just a way to draw nodes with an easier way to position them.

A matrix is a node with other nodes inside. It's an option to keep several nodes together. But you cannot nest matrix nodes. Although you can join several matrix nodes with fit.

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{matrix, fit, positioning}

\begin{document}
\begin{tikzpicture}[mymat/.style={matrix of nodes, draw, nodes={draw, anchor=center}, row sep=1pt}]
\matrix[mymat] (A) {
Some text\\
Some more text\\};


\matrix[mymat, below=of A] (B){
Some text & More text\\
Some more text\\};

\node[draw, fit=(A) (B)]{};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .