我想将一个节点分成 3 个部分:首先将其垂直均等分割,然后将底部节点水平均等地分成 2 个框。
在 tikz 中,这种情况的最佳解决方案是什么?也许我们可以只画 3 个框并仔细对齐,但对齐任务对我来说似乎很难:)
下面是测试代码,但看起来 B 和 C 没有与 A 的底部对齐,也没有平均分割:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,positioning}
\usepackage{amsmath,bm,times}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\begin{document}
\tikzstyle{box}=[draw, fill=pink!10, minimum width=5em,
text centered, minimum height=2.5em,drop shadow]
\begin{tikzpicture}[node distance=0cm,outer sep = 0pt]
\node (A) [box,text width=10em] {A};
\node (B) [box,below= of A.south west,anchor=west] {B};
\node (C) [box,below= of A.south east,anchor=east] {C};
\end{tikzpicture}
\end{document}
输出图片如下:
答案1
经过最少的修改后修复:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,positioning}
\usepackage{amsmath,bm,times}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\begin{document}
\tikzstyle{box}=[draw, fill=pink!10, minimum width=5em,
text centered, minimum height=2.5em,drop shadow]
\begin{tikzpicture}[node distance=0cm,outer sep = 0pt]
\node (A) [box,minimum width=10em] {A};
\node (B) [box,anchor=north west,minimum width=5em] at (A.south west) {B};
\node (C) [box,anchor=north east,minimum width=5em] at (A.south east) {C};
\end{tikzpicture}
\end{document}
问题你的解决方案是您想控制对应于 的框的大小minimum width
,但您却使用text width
来控制内容文本的大小而不是框的大小。因此,text width
被 填充inner sep
。节点(B)
和(C)
实际上具有相同的大小,但(C)
与 重叠(B)
。
答案2
非常好,大家的建议都很棒!
以下是正确解决我的问题的总结版本:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,positioning}
\usepackage{amsmath,bm,times}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\begin{document}
\tikzset{box/.style={draw, fill=pink!10, minimum width=5em,
text centered, minimum height=2.5em,inner sep=0pt}}
\newcommand{\trinode}[4] {%
\node (#1) [box,minimum width=#4] {#1};
\node (#2) [box,minimum width=0.5*#4,below=of #1.south west,
anchor=north west] {#2};
\node (#3) [box,minimum width=0.5*#4,below=of #1.south east,
anchor=north east] {#3};
}
\begin{tikzpicture}[node distance=0cm,outer sep = 0pt]
\trinode{A}{BBBB}{CCCC}{10em};
\def\dx{2cm}
\draw [red,shorten >=-\dx,shorten <=-.1*\dx] (A.south) -- (A.north);
\end{tikzpicture}
\end{document}
最终输出如下所示: