![Tikz 树的边缘始终连接到元素顶部中心](https://linux22.com/image/405631/Tikz%20%E6%A0%91%E7%9A%84%E8%BE%B9%E7%BC%98%E5%A7%8B%E7%BB%88%E8%BF%9E%E6%8E%A5%E5%88%B0%E5%85%83%E7%B4%A0%E9%A1%B6%E9%83%A8%E4%B8%AD%E5%BF%83.png)
\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fit,shapes.geometric}
\usepackage{amssymb}
\begin{document}
\begin{tikzpicture}[]
\tikzstyle{tree_op}=[rectangle,draw=black,fill=gray!30,thick,minimum size=6mm,inner sep=0pt]
\tikzstyle{tree_subtree}=[rectangle,draw = black,densely dotted,fill=gray!30,thick,minimum size=6mm,inner sep=0pt]
\tikzstyle{triangle}=[inner sep=0pt,text=red,draw, regular polygon, regular polygon sides=3, align=center]
\tikzstyle{level 1}=[sibling distance=14mm,level distance=10mm]
\tikzstyle{level 2}=[sibling distance=14mm]
\tikzstyle{level 3}=[sibling distance=20mm]
\tikzstyle{level 4}=[sibling distance=11mm,level distance=11mm]
\node [tree_op] (root){$\rightarrow $}
child {node [triangle] (t1) {$\triangle_1$}}
child {node [triangle] (ti) {$\triangle_i$}}
child {node [triangle] (tj) {$\triangle_j$}}
child {node [triangle] (tn) {$\triangle_n$}}
;
\end{tikzpicture}
\end{document}
我对当前结果有两个疑问:
- 三角形有不同的尺寸
- 边应该连接到三角形的顶点
答案1
为了解决您的问题,我添加了
- 一种
equal size
样式(依赖于eqparbox
),确保节点具有相同的大小,并且 child anchor=north
。
请注意,\tikzstyle
已弃用,这就是我用相应\tikzset
语法替换它的原因。(我还增加了,level distance
但当然,如果您不喜欢,您可以撤消它。)
\documentclass[]{standalone}
\usepackage{tikz}
\usepackage{eqparbox}
\newbox\nodebox
\usetikzlibrary{trees,shapes.geometric}
\usepackage{amssymb}
\begin{document}
\begin{tikzpicture}[]
\tikzset{tree_op/.style={rectangle,draw=black,fill=gray!30,thick,minimum size=6mm,inner sep=0pt},
tree_subtree/.style={rectangle,draw = black,densely dotted,fill=gray!30,thick,minimum size=6mm,inner sep=0pt},
triangle/.style={inner sep=0pt,text=red,draw, regular polygon, regular polygon
sides=3, align=center,equal size=T},
level 1/.style={sibling distance=14mm,level distance=14mm},
level 2/.style={sibling distance=14mm},
level 3/.style={sibling distance=20mm},
level 4/.style={sibling distance=11mm,level distance=11mm},
equal size/.style={execute at begin
node={\setbox\nodebox=\hbox\bgroup},
execute at end
node={\egroup\eqmakebox[#1][c]{\copy\nodebox}}}}
\node [tree_op] (root){$\rightarrow $}
[child anchor=north]
child {node [triangle] (t1) {$\triangle_1$}}
child {node [triangle] (ti) {$\triangle_i$}}
child {node [triangle] (tj) {$\triangle_j$}}
child {node [triangle] (tn) {$\triangle_n$}}
;
\end{tikzpicture}
\end{document}