我在工作中尝试使用 LaTeX 的森林包在论文上显示分类树。到目前为止,我有以下代码:
\begin{forest}
for tree={grow=0, s sep=0pt,
edge= thick,
anchor=base west,
font=\strut\footnotesize\sffamily},
%
[,phantom % Paranormal root (BOO!)
%
[2E-VRP % Real branch
[One,tier=b,
[Yes, tier=c]
[No, tier=c]
]
[Two,tier=b,
[Yes, tier=c]
[No, tier=c,
[{ex1 ex2 ex3}, tier=d]]
]
]
%
[T1,for tree={color=gray,no edge}
[T2, tier=b % Ethereal branch
[T3, tier=c,
[T4, tier=d]]]]
%
] % Closing poltergeist
%
\end{forest}
得到如下图所示:
我怎样才能让 ex1、ex2 和 ex3 彼此位于下方,即位于不同的行上?
答案1
您需要添加align=left
到for tree
选项,然后写入ex1\\ ex2\\ ex3
。通过进一步的一些小简化,您的 MWE 可以是:
编辑:
考虑一下您在以下评论中的愿望:
\documentclass[border=3.141592mm]{standalone}
\usepackage{forest}
\tikzset{
LBL/.style = {font=\footnotesize\sffamily, text=gray, above=5mm}
}
\begin{document}
\begin{forest}
for tree={
align=left,
font=\footnotesize\sffamily,
anchor=west,
% tree style
grow=0,
edge= thick,
l sep=4mm,
s sep=4mm,
},
[2E-VRP, name=l1
[One,
[Yes]
[No]
]
[Two, name=l2
[Yes]
[No, name=l3
[ex1\\ ex2\\ ex3, name=l4]
]
]
]
\path (l1 |- l4) node[LBL] {T1}
(l2 |- l4) node[LBL] {T2}
(l3 |- l4) node[LBL] {T3}
(l4) node[LBL] {T4};
\end{forest}
\end{document}
附录:
如果您想要镜像图,则需要grow=0˙ with ˙grow'=0
相应地替换和移动节点名称(该级别标签将保留在图的顶部):
\documentclass[border=3.141592mm]{standalone}
\usepackage{forest}
\tikzset{
LBL/.style = {font=\footnotesize\sffamily, text=gray, above=5mm}
}
\begin{document}
\begin{forest}
for tree={
align=left,
font=\footnotesize\sffamily,
anchor=west,
% tree style
grow'=0,
edge= thick,
l sep=4mm,
s sep=4mm,
},
[2E-VRP, name=l1
[One, name=l2
[Yes, name=l3]
[No]
]
[Two
[Yes]
[No
[ex1\\ ex2\\ ex3, name=l4]
]
]
]
\path (l1 |- l3) node[LBL] {T1}
(l2 |- l3) node[LBL] {T2}
(l3) node[LBL] {T3}
(l4 |- l3) node[LBL] {T4};
\end{forest}
\end{document}
答案2
您可以使用 添加换行符并添加对齐[{ex1 \\ ex2 \\ ex3}, tier=d, align=left]
。但我猜,这需要一些额外的垂直移位。
更简洁的方法可能是将 ex1、ex2 和 ex3 添加为父节点的子节点,删除 ex1 和 e3 的边并将它们移得更近一些:
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow=0, s sep=0pt,
edge= thick,
anchor=base west,
font=\strut\footnotesize\sffamily},
%
[,phantom % Paranormal root (BOO!)
%
[2E-VRP % Real branch
[One,tier=b,
[Yes, tier=c]
[No, tier=c]
]
[Two,tier=b,
[Yes, tier=c]
[No, tier=c, s sep=-10pt
[ex3, tier=d, no edge ]
[ex2, tier=d ]
[ex1, tier=d, no edge ] ]
]
]
%
[T1, for tree={color=gray, no edge}
[T2, tier=b % Ethereal branch
[T3, tier=c,
[T4, tier=d]]]]
%
] % Closing poltergeist
%
\end{forest}
\end{document}