我开始了,但几步之后就失败了,有人能帮我吗?我想从中学习https://latexdraw.com/draw-trees-in-tikz/,但不知何故经过两步之后却无法生成。
\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
[
level 1/.style = {teal, sibling distance = 4cm},
level 2/.style = {teal, sibling distance = 4cm},
level 3/.style = {teal, level distance = 1cm},
edge from parent fork down
]
\node {\textcolor{teal}{1}}
child {node [draw]{2}
edge from parent [left]}
child {node [draw] {3}
child {node [draw] {4}
child {node [draw] {5}}}
child {node [draw] {6}}
edge from parent node [right] {}};
\end{tikzpicture}
\end{document}
答案1
像这样?
使用forest
包装很容易:
\documentclass[border=0.2cm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
% style of nodes in the tree
draw, semithick,
minimum height = 3ex,
minimum width = 2em,
% style of tree (edges, distances, direction)
grow = south,
forked edge, % for forked edge
s sep = 3mm, % "sibling" distance
l sep = 6mm, % "level" distance
fork sep = 3mm, % distance from parent to branching point
}
[1
[2]
[3,fit=band
[1
[5
[9]
[10]
]
[6
[11]
[12]
]
]
[5
[7
[13]
[14]
]
[8
[15]
[16]
]
]
]
]
\end{forest}
\end{document}
编辑:
如果您希望图表更加丰富多彩,您只需向节点元素添加所需的颜色(例如draw=<desired color>
,fill=` 等)。您还可以更改连接线(边)的外观。请参阅下面的 MWE:
\documentclass[border=0.2cm]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta} % <---
\begin{document}
\begin{forest}
for tree={
% style of nodes in the tree
draw=blue, % <---
thick,
fill=blue!10, % <---
text=red, % <---
minimum height = 3ex,
minimum width = 2em,
% style of tree (edges, distances, direction)
edge={semithick, -{Straight Barb[scale=0.8]},draw=teal}, % <---
forked edge, % for forked edge
l sep = 9mm, % "level" distance
s sep = 3mm, % "sibling" distance
where level = 4{s sep=1mm}{},
fork sep = 3mm, % distance from parent to branching point
}
[1
[2]
[3,fit=band
[1
[5
[9]
[10]
]
[6
[11]
[12]
]
]
[5
[7
[13]
[14]
]
[8
[15]
[16]
]
]
]
]
\end{forest}
\end{document}