4

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

stacked two nodes

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.

1
  • I do not like to use fixed sizes because that courses too much hand work when writing huge stuff. Apr 15, 2015 at 8:52

2 Answers 2

6

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:

enter image description here

4
  • @Grisu: Glad it helps :) Apr 15, 2015 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. Apr 15, 2015 at 9:17
  • @Grisu: Have you loaded the shapes.multipart library in the preamble? Apr 15, 2015 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. Apr 15, 2015 at 11:08
4

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}

enter image description here

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

You must log in to answer this question.

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