我们怎样才能将 A 移至中心?
\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\usepackage[T1]{fontenc}
\tikzset{%
parent/.style={align=center,text width=3cm,rounded corners=3pt},
child/.style={align=center,text width=3cm,rounded corners=3pt}
}
\begin{document}
\begin{forest}
for tree={
% edge+={->},% uncomment for arrows
draw,
rounded corners,
node options={align=center,},
text width=2.7cm,
},
where level=0{%
parent anchor=children,
}{%
folder,
grow'=0,
if level=1{% this changes the edges from level 0 to nodes at level 1
before typesetting nodes={child anchor=north},
edge path'={(!u.parent anchor) -- ++(0,-5pt) -| (.child anchor)},
}{},
}
[A, fill=gray!25, parent
[B, for tree={fill=brown!25, child}
]
[C, for tree={fill=blue!25, child}
]
]
]
\end{forest}
\end{document}
答案1
您的代码对于这棵简单的树来说没有必要那么复杂。
\documentclass[border=10pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
for tree={
% nodes
draw,
rounded corners,
fill=gray!25,
text width=3 cm,
text centered,
% tree
grow=270,
parent anchor=south,
child anchor=north,
% edge = {-Straight Barb},
forked edge,
l sep=8mm,
fork sep=4mm,
s sep=2mm
}
[A
[B, fill=brown!25]
[C, fill=blue!25]
]
\end{forest}
\end{document}