TIKZ:堆叠两个宽度相等的节点,无需双重绘制重叠的形状

TIKZ:堆叠两个宽度相等的节点,无需双重绘制重叠的形状

我正在尝试将 2 个 Tikz 节点直接堆叠在一起,中间没有空间,并且它们之间没有双倍厚度的形状。此外,两个节点应该具有相同的宽度,要么是固定的,要么由底部节点的宽度决定。我的第一次尝试是:

\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}

其结果是

堆叠两个节点

这看起来不像我想要的。我需要上部节点与下部节点一样宽,并且中间没有双线。或者,构造一个可以实现类似外观的结构。

答案1

我需要上部节点与下部节点一样宽,并且它们中间没有双线。

因此,最简单的方法不是创建单独的形状,而是使用旧的且非常有用的形状,rectangle split这可以自动帮助避免双线并保留两个部分的宽度。

可能的解决方案:

\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}

结果:

在此处输入图片描述

答案2

row sep=\-pgflinewidth在选项中设置matrix。如果所有节点都具有相同的功能,它将起作用line width

every node={minimum width=4cm, draw}声明中两个节点可以具有相同的宽度matrix

\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}

在此处输入图片描述

相关内容