1

I have made a fitted node around Node1, and I'd like to add another node directly on top of fittedNode with the same width as the fittedNode.

This is what I have so far:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz} % Tikz diagrams
\usetikzlibrary{backgrounds,fit}


\begin{document}


\begin{tikzpicture}
    \node[draw=black, fill=white] at (0,0) (Node1) {Node 1};
    
    \begin{scope}[on background layer]
        \node[draw=black, fill=blue!10, fit=(Node1.south west) (Node1.north east)] (fittedNode) {};
    \end{scope}

    \node[draw=black, above] at (fittedNode.north) (Node2) {Node 2}; 
\end{tikzpicture}


\end{document}

enter image description here

but Node2 does not have the same width as fittedNode, and also the bottom line of Node2 overlaps with the top line of the fittedNode making the overall line appear thicker, whereas they should just share a single line there.

1
  • 2
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Dec 5, 2024 at 13:23

2 Answers 2

3

To ensure that Node2 has the same width as fittedNode and prevent overlapping lines, you can control the node's width and adjust its position manually. Here's how to achieve this:

  1. Set the minimum width property of Node2 to match fittedNode's width.
  2. Add a small vertical shift to position Node2 so that it doesn't overlap with fittedNode.

Here's an improved version of your code:

\documentclass[tikz]{standalone}

\usetikzlibrary{positioning, backgrounds, fit}

\begin{document}
    
\begin{tikzpicture}[
    innerNode/.style={
        draw,
        fill=white,
        minimum width = 2cm, % <-- change here
    }
]
    \node [innerNode] at (0,0) (Node1) {Node 1};
    
    \begin{scope}[on background layer]
        \node [draw, fill=blue!10, fit=(Node1), inner xsep=5mm] (fittedNode) {}; % <--- change here
    \end{scope}
    
    \node [innerNode, above=0mm of fittedNode.north, yshift=-\pgflinewidth, minimum width=3cm + \pgflinewidth] (Node2) {Node 2}; 
\end{tikzpicture}


\end{document}

This is the result:

enter image description here

Edit

In addition to the previously suggested approach, here is another method that achieves the desired result. This solution uses TikZ's let syntax to dynamically compute the width of fittedNode and apply it to Node2. Here's the complete code:

\documentclass[tikz]{standalone}

\usetikzlibrary{positioning, backgrounds, fit, calc}

\begin{document}

\begin{tikzpicture}
    \node[draw=black, fill=white] at (0,0) (Node1) {Node 1};
    
    \begin{scope}[on background layer]
        \node[draw=black, fill=blue!10, fit=(Node1.south west) (Node1.north east)] (fittedNode) {};
    \end{scope}
    
    \path let \p1=(fittedNode.west),
              \p2=(fittedNode.east),
              \n1 = {\x2-\x1-\pgflinewidth} in
          node (Node2) [draw, above=0mm of fittedNode,
                yshift=-\pgflinewidth, minimum width=\n1,
               ] {Node 2}; 
               
\end{tikzpicture}

\end{document}

enter image description here

3
  • 2
    That would ruin the purpose of making a fit in the first place Commented Dec 5, 2024 at 14:16
  • Yes, the yshift operation is helpful, but I can't use a minimum width because the required width is unknown beforehand; it's calculated using the fit function. Once the fitted node has been made, is there a way to obtain its width? Commented Dec 5, 2024 at 14:22
  • The let syntax approach is also demonstrated here: tex.stackexchange.com/questions/23480/…. Commented Dec 5, 2024 at 14:46
4

My library ext.positioning-plus includes the fit's library functionalities into those of the positioning library.

With above = … of -(<node>) you not only place a node above of <node> but also make it as wide as <node>. (Alternatively, | makes it as high and + makes it as wide and high.)

The last node will be placed -\pgflinewidth above of the fitted node since you want the lines to overlap. An alternative approach would be to use outer sep=+0pt and above = 0pt of … but that changes the dimensions of the nodes slightly because the outer sep is included in the size that is considered by the fit library.

Code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\usetikzlibrary{ext.positioning-plus}
\begin{document}
\begin{tikzpicture}
\node[draw=black, fill=white] at (0,0) (Node1) {Node 1}
 node[draw=black, fill=blue!10, behind path, fit=(Node1)] (fittedNode) {}
 node[draw=black, above=-\pgflinewidth of -(fittedNode)] (Node2) {Node 2};
\end{tikzpicture}

\begin{tikzpicture}[outer sep=+0pt]
\node[draw=black, fill=white] at (0,0) (Node1) {Node 1}
 node[draw=black, fill=blue!10, behind path, fit=(Node1)] (fittedNode) {}
 node[draw=black, above=0pt of -(fittedNode)] (Node2) {Node 2};
\end{tikzpicture}
\end{document}

Output

enter image description here

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.