我正在尝试使用 绘制一棵简单的树forest
。简化版本如下:
现在我需要减少图形占用的水平空间。我的想法是将根节点移到节点 A 上方,因为根节点下方的所有空间都未使用。我尝试在绘制树之前手动调整根节点的位置,但我得到的结果是:
我需要一个不同的、更复杂的从根到其子节点的边缘定位(如下图中手绘红线所示)。
任何建议都将不胜感激。谢谢。
以下是此示例的代码:
\documentclass[tikz,border=10pt,multi,dvipsnames,svgnames,x11names]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
forked edges,
for tree={
calign=first,
anchor=west,
fit=rectangle,
draw=gray,
grow'=0,
edge={gray,line width=1pt},
},
[Root, before drawing tree={y+=3.8em, x+=3em},
[{Node A}
[A1]
[A2]
[A3]
]
[{Node B}
[B1]
[B2]
[B3]
]
]
\end{forest}
\end{document}
答案1
最终我解决了我的问题如下:
\documentclass[tikz,border=10pt,multi,dvipsnames,svgnames,x11names]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
forked edges,
for tree={
calign=first,
anchor=west,
fit=rectangle,
draw=gray,
grow'=0,
if level=0 {parent anchor=south, child anchor=parent}{},
if level=1 {edge={red,line width=1pt},edge path'={(!u.parent anchor)|-(.child anchor)}}{ edge={gray,line width=1pt} }
},
[Root, before drawing tree={y+=3.8em, x+=2em},
[{Node A}
[A1]
[A2]
[A3]
]
[{Node B}
[B1]
[B2]
[B3]
]
]
\end{forest}
\end{document}
请注意,我还必须将根节点稍微向左移动。
答案2
\documentclass[tikz,border=10pt,multi,dvipsnames,svgnames,x11names]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
forked edges,
% /.style={
% for tree={parent anchor=children},
% for descendants={child anchor=parent,forked edge}
%}
for tree={
calign=first,
% parent anchor=east,
% child anchor=east,
fit=rectangle,
draw=gray,
grow'=0,
edge={gray,line width=1pt},edge path'={(!u.parent anchor)--($(!u.parent anchor)+(4pt,0pt)$) -- ++(0pt,-15pt) -| ($(-2pt,0pt)+(.child anchor)$)--(.child anchor)},
},
[Root, before drawing tree={y+=3.8em, x+=3em},
[{Node A}
[A1]
[A2]
[A3]
]
[{Node B}
[B1]
[B2]
[B3]
]
]
\end{forest}
\end{document}