我有以下示例。
\documentclass[a4paper,10pt]{report}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
forked edges,
/tikz/every pin edge/.append style={Latex-, shorten <=2.5pt, darkgray},
/tikz/every pin/.append style={darkgray, font=\sffamily},
/tikz/every label/.append style={darkgray, font=\sffamily},
before typesetting nodes={
delay={
where content={}{coordinate}{},
},
where n children=0{tier=terminus, label/.process={Ow{content}{right:#1}}, content=}{},
},
for tree={
grow'=0,
s sep'+=10pt,
l sep'+=25pt,
},
l sep'+=50pt,
[, !l.edge label={coordinate [pos=0, pin=-135:common ancestor] }, !1.edge label={node [pos=.65, every label, above] {branch}}, !11.edge label={coordinate [pos=0, pin={[pin distance=30pt, align=center]135:single merger}]}
[ [[6][[5][4]] ] [3]]
[[2][1]]
]
\end{forest}
\end{document}
合并 {1,2} 和 {{3},{2,5,6}} 时可以有不同的位置吗?我现在正在阅读森林包的文档,但还没有找到任何线索来说明如何修改它。一个好的结果应该是这样的。
答案1
您可以创建一个自定义样式,分别移动当前级别的父锚点和下一级别的子锚点的坐标(但对于相对较大的连续移动,它可能无法正常工作):
\documentclass[a4paper,10pt]{report}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}
\forestset{
}
\begin{document}
\begin{forest}
forked edges,
/tikz/every pin edge/.append style={Latex-, shorten <=2.5pt, darkgray},
/tikz/every pin/.append style={darkgray, font=\sffamily},
/tikz/every label/.append style={darkgray, font=\sffamily},
before typesetting nodes={
delay={
where content={}{coordinate}{},
},
where n children=0{
tier=terminus,
label/.process={Ow{content}{right:#1}},
content={}
}{},
},
for tree={
grow'=0,
s sep'+=10pt,
l sep'+=25pt,
},
shift fork/.style={
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor)
-- ++(\forestoption{fork sep},0)
|- ([xshift={#1}].child anchor)
\forestoption{edge label};
},
delay={
for children={
edge path={
\noexpand\path[\forestoption{edge}]
([xshift={#1}]!u.parent anchor)
-- ++(\forestoption{fork sep},0)
|- (.child anchor)
\forestoption{edge label};
},
}
}
},
[, !l.edge label={coordinate [pos=0, pin=-135:common ancestor]},
!1.edge label={node [pos=.65, every label, above left] {branch}},
!11.edge label={coordinate [pos=0, pin={
[pin distance=30pt, align=center]135:single merger
}]}
[
[, shift fork=15pt
[[6][7]]
[, shift fork=-5pt [5][4]]
]
[[3][3]]
]
[, shift fork=-10pt [2][1]]
]
\end{forest}
\end{document}