# TIKZ: Stacking two nodes with equal width without double drawing the overlapping shapes

I am trying to stack 2 Tikz nodes directly below each other with no space and without having the double-thick shape between them. Furthermore, both nodes should have the same width, either fixed or determined by the with of the bottom node. My First try is:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{recttop}{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\backgroundpath{% this is new
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% construct main path
\pgfsetcornersarced{\pgfpoint{5pt}{5pt}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfsetcornersarced{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
}
}
\pgfdeclareshape{rectbottom}{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\backgroundpath{% this is new
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% construct main path
\pgfsetcornersarced{\pgfpoint{0pt}{0pt}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfsetcornersarced{\pgfpoint{5pt}{5pt}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
}
}
\makeatother

\begin{document}
\begin{tikzpicture}
%[every node/.style={fill=gray,minimum width=2cm, minimum height=1cm}]
\matrix[row sep=0pt, every node/.style={draw}]{
\node[draw, shape=recttop, fill=red!20] (top) {CFunction};\\
\node[draw, shape=rectbottom] (bottom) {\ttfamily func(A, E, B, Z);};\\
};
\end{tikzpicture}
\end{document}


Which results in

which does not look like I want it. I need the upper node as wide as the lower one and without the double drawn line in the middle of them. Alternatively a construct which realized a similar look.

• I do not like to use fixed sizes because that courses too much hand work when writing huge stuff. – M.K. aka Grisu Apr 15 '15 at 8:52

I need the upper node as wide as the lower one and without the double drawn line in the middle of them.

Hence, the simplest way is not to create separate shapes, but rather use the old and very useful rectangle split which automatically helps avoiding the double line and preserves the width for both parts.

A possible solution:

\documentclass[tikz,border=10pt]{standalone}

\usetikzlibrary{shapes.multipart}

\tikzset{my rect/.style={
rectangle split,
rectangle split parts=2,
draw,
rounded corners,
}
}

\begin{document}

\begin{tikzpicture}
\node[my rect,rectangle split part fill={red!20,white}]{CFunction\nodepart{two}\ttfamily func(A, E, B, Z);};
\end{tikzpicture}

\end{document}


The result:

• @Grisu: Glad it helps :) – Claudio Fiandrino Apr 15 '15 at 8:52
• But moving it to the final document, which have to use the siamltex1213 class, this does not work. It compiles with out any error but the no shape appears and the top text is overwritten by the bottom one. Alternatively (Latex->dvips) the shapes appear but the text is placed at the wrong position. – M.K. aka Grisu Apr 15 '15 at 9:17
• @Grisu: Have you loaded the shapes.multipart library in the preamble? – Claudio Fiandrino Apr 15 '15 at 9:45
• Yes I do. But there seems to be many strange other problems with this class, which I have to figure out before. – M.K. aka Grisu Apr 15 '15 at 11:08

Set row sep=\-pgflinewidth in matrix options. It will work if all nodes have same line width.

Both nodes can have same width with every node={minimum width=4cm, draw} in matrix declaration.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{recttop}{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\backgroundpath{% this is new
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% construct main path
\pgfsetcornersarced{\pgfpoint{5pt}{5pt}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfsetcornersarced{\pgfpoint{0pt}{0pt}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
}
}
\pgfdeclareshape{rectbottom}{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\backgroundpath{% this is new
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% construct main path
\pgfsetcornersarced{\pgfpoint{0pt}{0pt}}
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfsetcornersarced{\pgfpoint{5pt}{5pt}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
}
}
\makeatother

\begin{document}
\begin{tikzpicture}
%[every node/.style={fill=gray,minimum width=2cm, minimum height=1cm}]
\matrix[row sep=0pt, every node/.style={minimum width=4cm, draw}, row sep=-\pgflinewidth]{
\node[draw, shape=recttop, fill=red!20] (top) {CFunction};\\
\node[draw, shape=rectbottom] (bottom) {\ttfamily func(A, E, B, Z);};\\
};
\end{tikzpicture}
\end{document}


• That only solves the first part of the problem. But I need the top shape as wide as the bottom shape as well. – M.K. aka Grisu Apr 15 '15 at 8:50
• @Grisu Answer updated – Ignasi Apr 15 '15 at 8:53