答案1
如果forest
是选项,它将自动调整树的布局以适应节点。它还允许非常简洁地指定树,并且在树的外观方面非常可定制。例如:
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
grow'=0,
math content,
tier/.wrap pgfmath arg={tier #1}{level()},
l sep+=5mm,
},
before typesetting nodes={
for tree={
if n=1{
edge label/.wrap value={node [midway, font=\scriptsize, above] {$#1$}}
}{
edge label/.wrap value={node [midway, font=\scriptsize, anchor=north] {$#1$}}
}
}
}
% now to specify the tree itself - as you see, the syntax is quite concise
[r
[C^h, edge label=P^h
[C^h, edge label=P^h]
[C^l, edge label=P^l]
]
[C^l, edge label=P^l
[C^h, edge label=P^h]
[C^l, edge label=P^l]
]
]
\end{forest}
\end{document}
编辑
有评论问没有根节点怎么办。任何树都必须有根,但它可以是不可见的、不占用空间的根。Forestphantom
为这种情况提供了选项:
\begin{forest}
for tree={
grow'=0,
math content,
tier/.wrap pgfmath arg={tier #1}{level()},
l sep+=5mm,
},
before typesetting nodes={
for tree={
if n=1{
edge label/.wrap value={node [midway, font=\scriptsize, above] {$#1$}}
}{
edge label/.wrap value={node [midway, font=\scriptsize, anchor=north] {$#1$}}
}
}
}
[, phantom
[C^h, edge label=P^h
[C^h, edge label=P^h]
[C^l, edge label=P^l]
]
[C^l, edge label=P^l
[C^h, edge label=P^h]
[C^l, edge label=P^l]
]
]
\end{forest}