You can use yshift
together with fit
and inner sep=0pt
to get a node of the same height and width as the other nodes, but shifted vertically. Note that the placement of the node text is different than in a normal node, so I would suggest you use the label=center:<text>
option to place the text instead. As Martin points out, you should also set the outer sep
of the nodes you're fitting around to 0pt
, as otherwise your new node will be too large by \pgflinewidth
.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit}
\begin{document}
\begin{tikzpicture}[node distance=0.2cm,mynode/.style={outer sep=0pt, draw}]
\node[mynode] (foo) {foo};
\node[mynode] (bar) [right=of foo] {bar};
\node [
mynode,
inner sep=0pt,
yshift=-1cm,
fit={(foo) (bar)},
label=center:foobar] {};
\end{tikzpicture}
\end{document}
Below is an approach to get the vertical spacing between the old nodes and the newly created one right. Using the calc
library, you can shift the new node down by the height of the old nodes by using ($(foo.south) - (foo.north)$)
You can't directly read the value of node distance
, so I've appended code to store the value in a new key that can be read in a yshift
.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\pgfkeys{
/tikz/node distance/.append code={
\pgfkeyssetvalue{/tikz/node distance value}{#1}
}
}
\begin{document}
\begin{tikzpicture}[
node distance=0.2cm,
mynode/.style={
draw,
outer sep=0pt
}]
\node[mynode] (foo) {foo};
\node[mynode] (bar) [right=of foo] {bar};
\node [
mynode,
inner sep=0pt,
shift=($(foo.south)-(foo.north)$),
yshift=-\pgfkeysvalueof{/tikz/node distance value},
fit={(foo) (bar)},
label=center:foobar] {};
\end{tikzpicture}
\end{document}
A different approach is using the let
syntax to calculate the difference between bar.east
and foo.west
, and using that to set the minimum width
of the new node:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[node distance=0.2cm,mynode/.style={rectangle,draw}]
\node[mynode] (foo) {foo};
\node[mynode] (bar) [right=of foo] {bar};
\path let
\p1=(foo.west),
\p2=(bar.east)
in node [
mynode,
below=of foo.south west,
anchor=north west,
minimum width=\x2-\x1-\pgflinewidth
] {foobar};
\end{tikzpicture}
\end{document}