这是对以下问题的后续回答:是否可以使用森林包更改某些树级别的边缘?因为缺少后续问题选项。
下面修改后的 MWE 几乎 100% 满足了我的要求,但在最后一级的父级和相应的边缘路径的锚定方面仍然需要进行细微的调整。
为什么边缘路径在第一个forest
向东生长的层级中工作正常,但在第二个向东生长east
的最后一层级中却不行?第二个向东生长的最后一层级的工作方式应该完全相同吗?forest
edge path
forest
另外,我尝试将子节点向下推(创建屏幕截图后!),但该s
选项似乎仅适用于底部子节点 N1C2,而不适用于顶部节点 N1C1。
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
draw=black,align=center,l sep=4em,grow=east,parent anchor=east,child anchor=west,
where n children=0{draw=red,for parent={calign=last}}{},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(15pt,0) |- % used with east-west growth
(.child anchor)\forestoption{edge label};
}
}
[P\\1\\2
[N1\\1\\2,name=_N1
[N1C1\\1\\2]
[N1C2,name=_N2]
]
[N2
[N2C1]
[N2C2]
]
]
\end{forest}
\begin{forest}
for tree={
draw=black,align=center,l sep=4ex,
where n children=0{
child anchor=west,
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(15pt,0) |- % used with east-west growth
(.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=east,
grow'=east,calign=first,}}{},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-25pt) -| % used with normal north-south growth
(.child anchor)\forestoption{edge label};
}
}
[P\\1\\2
[N1\\1\\2,name=_N1
[N1C1\\1\\2,s=-8ex]
[N1C2,name=_N2,s=-16ex]
]
[N2
[N2C1]
[N2C2]
]
]\end{forest}
\end{document}
更新:
经过进一步调整后,下面的代码与理想解决方案相差无几。请注意:前 2 个级别不能硬编码,可以有任意数量,并且此问题中的问题仅适用于红色的最后一级。
仅剩的两个问题是如何将红色节点向下推到其父节点下方?
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
draw=black,align=center,l sep=4ex,parent anchor=south,child anchor=north,
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| % used with normal north-south growth
(.child anchor)\forestoption{edge label};
},
where n children=0{
child anchor=west,node options={minimum width=22em}, % to make nodes same width
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(-1em,0) |- % used with east-west growth
(.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=south,l sep=1em,
grow'=east,calign=child edge%first
}
}{}
}
[P0\\1\\2
[N111111111111111111111\\1\\2,name=N1
[N1C1\\1\\2,name=N1C1]
[N1C2\\1\\2,name=N1C2]
]
[N2222222222\\1\\2,name=N2
[N2C1\\1\\2,name=N2C1]
[N2C2\\1\\2,name=N2C2]
]
[N3333333333333\\1\\2,name=N3
[N3C1.............\\1\\2,name=N3C1]
[N3C2....\\1\\2,name=N3C2]
[N3C3........\\1\\2,name=N3C3]
]
]
\end{forest}
\end{document}
答案1
您可以在任何特定级别使用text width
(或minimum width
) 键来保证该级别的节点具有恒定的宽度;要将子项向下推,最简单的方法是使用phantom
节点。
\documentclass[border=3pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
draw=black,align=center,l sep=4ex,parent anchor=south,child anchor=north,
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| % used with normal north-south growth
(.child anchor)\forestoption{edge label};
},
where n children=0{
child anchor=west,
text width=1.5cm,
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) |- % used with east-west growth
(.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=south,l sep=1em,
grow'=east,calign=child edge%first
}
}{}
}
[P0\\1\\2
[N111111111111111111111\\1\\2,name=N1
[,phantom]
[N1C1\\1\\2,name=N1C1]
[N1C2\\1\\2,name=N1C2]
[N1C3\\1\\2,name=N1C3]
]
[N2222222222\\1\\2,name=N2
[,phantom]
[N2C1\\1\\2,name=N2C1]
[N2C2\\1\\2,name=N2C2]
]
[N3333333333333\\1\\2,name=N3
[,phantom]
[N3C1.............\\1\\2,name=N3C1]
[N3C2....\\1\\2,name=N3C2]
[N3C3........\\1\\2,name=N3C3]
]
]
\end{forest}
\end{document}
或者,使用for children
(分别是for descendants
)键仅使一个节点影响其子节点(分别是其后代)而不是整个级别:
\documentclass[border=3pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
draw=black,align=center,l sep=4ex,parent anchor=south,child anchor=north,
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| % used with normal north-south growth
(.child anchor)\forestoption{edge label};
},
where n children=0{
child anchor=west,
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) |- % used with east-west growth
(.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=south,l sep=1em,
grow'=east,calign=child edge%first
}
}{}
}
[P0\\1\\2
[N111111111111111111111\\1\\2,name=N1
[,phantom]
[N1C1\\1\\2,name=N1C1]
[N1C2\\1\\2,name=N1C2]
[N1C3\\1\\2,name=N1C3]
]
[N2222222222\\1\\2,name=N2
[,phantom]
[N2C1\\1\\2,name=N2C1]
[N2C2\\1\\2,name=N2C2]
]
[N3333333333333\\1\\2,name=N3,for children={text width=2.5cm}
[,phantom]
[N3C1.............\\1\\2,name=N3C1]
[N3C2....\\1\\2,name=N3C2]
[N3C3........\\1\\2,name=N3C3]
]
]
\end{forest}
\end{document}