答案1
当然。为了保持一致性,我倾向于将第一个子项的边缘弄成正方形,尽管这不是必需的。(事实上,很多更容易不使用当前版本的 Forest,因为forked edges
它们folder
根本不能很好地相处!)
请注意,这需要 Forest 的第 2 版。您可以在第 1 版中执行相同操作,但它要复杂得多。
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
where level=1{
child anchor=north,
!u.parent anchor=south,
before computing xy={
l*=.5,
},
if={n==(int((n_children("!u")+1)/2))}{
calign with current edge
}{},
edge path'={(!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)},
for tree={
folder,
grow'=0
}
}{},
for tree={
edge={thick},
},
before typesetting nodes={
tempcounta'/.max={level}{root,tree},
for tree={
rounded corners,
top color/.wrap pgfmath arg={blue!#1!cyan}{100*((level())/((tempcounta)+1))},
+edge/.wrap pgfmath arg={blue!#1!cyan}{100*((level())/((tempcounta)+1))},
bottom color/.wrap pgfmath arg={blue!#1!cyan}{100*((level()+1)/((tempcounta)+1))},
draw/.wrap pgfmath arg={blue!#1!cyan}{100*((level()+1)/((tempcounta)+1))},
thick,
}
}
[Root
[A
[A1]
[A2]
]
[B
[B1
[a]
[b]
]
[B2]
]
[C
[C1]
[C2]
]
]
\end{forest}
\end{document}
着色只是为了好玩。
森林 1
如果您只有 Forest 1 版本且无法更新,则上述代码将出现编译错误。下面的代码灵活性稍差,但即使使用较旧的 Forest 也应该可以工作。(至少对我来说是有效的。)
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
if level=0{}{
if level=1{
tier=spread,
before typesetting nodes={
prepend={[, phantom]}
},
}{
child anchor=west,
edge path={
\noexpand\path [\forestoption{edge}] (!to tier=spread.south west) +(2.5pt,0) |- (.child anchor)\forestoption{edge label};
}
},
},
},
before packing={
where level=2{
if={n_children("!u")>1}{
if n=1{
grow=0,
}{
!u1.append=!n
}
}{}
}{}
},
where level=1{
child anchor=north,
!u.parent anchor=south,
if={n==(int((n_children("!u")+1)/2))}{
calign with current edge
}{},
edge path={
\noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)\forestoption{edge label};},
}{},
for tree={
rounded corners,
top color=white,
edge={thick, blue},
bottom color=blue,
thick,
draw=blue,
}
[Root
[A
[A1]
[A2]
]
[B
[B1]
[B2]
]
[C
[C1]
[C2]
]
]
\end{forest}
\end{document}