tikzpicture 中的边框高度不一致

tikzpicture 中的边框高度不一致

我定义了一个新命令,将一个矩形分成三部分,每部分包含一个标题。但是,中间部分不知怎么的。有人能解释一下为什么会这样吗?

\newcommand{\segmentation}[3]{
\begin{tikzpicture}
\node[text width=20mm, draw, align=center ] (first)  {#1};
\node[text width=22mm, align=center, right=0pt of first](second){#2};
\node[text width=35mm, align=center, right=0pt of second] (third) {#3};
\draw(second.north west) -- (second.north east) (second.south west)--(second.south east) (second.north east) -- (second.south east);
\draw(third.north west) -- (third.north east) (third.south west)--(third.south east) (third.north east) -- (third.south east);
\end{tikzpicture}
}

在此处输入图片描述

答案1

原因是中心节点包含“g”和“b”,因此文本占据的总高度比没有大下降字母的邻居更大。

有两个简单的解决方法:

1. 固定节点高度

\newcommand{\segmentation}[3]{
\begin{tikzpicture}[segment/.style={align=center, minimum height=2ex}]
\node[text width=20mm, draw, segment ] (first)  {#1};
\node[text width=22mm, segment, right=0pt of first](second){#2};
\node[text width=35mm, segment, right=0pt of second] (third) {#3};
\draw(second.north west) -- (second.north east) (second.south west)--(second.south east) (second.north east) -- (second.south east);
\draw(third.north west) -- (third.north east) (third.south west)--(third.south east) (third.north east) -- (third.south east);
\end{tikzpicture}
}

2. 在节点中放置支柱

\strut会插入一个不占用水平空间的隐形字母,其高度和深度与字体中最高和最深的字母相同。将其插入节点将使它们达到所需的高度以容纳任何文本

\newcommand{\segmentation}[3]{
\begin{tikzpicture}
\node[text width=20mm, draw, align=center ] (first)  {\strut#1};
\node[text width=22mm, align=center, right=0pt of first](second){\strut#2};
\node[text width=35mm, align=center, right=0pt of second] (third) {\strut#3};
\draw(second.north west) -- (second.north east) (second.south west)--(second.south east) (second.north east) -- (second.south east);
\draw(third.north west) -- (third.north east) (third.south west)--(third.south east) (third.north east) -- (third.south east);
\end{tikzpicture}
}

3. 多部分节点

看一下手册,有一个特殊的节点形状称为“多部分”,它可以执行你用宏模拟的操作,但以更通用的方式。

相关内容