我想在 Latex 中获得上面显示的树状图,但我不知道如何编辑它。我试图按照以下方式构建此图,但结果与原始图片不一样。我面临的问题是:
- 不知道如何调整同一层级下两个节点之间的距离;
- 我不知道如何像原始图片中那样装饰圆圈。
任何帮助将不胜感激。
\documentclass{article}
\usepackage{forest, color}
\usepackage{adjustbox}
\begin{document}
% Globally set style for your trees
\forestset{circles/.style={for tree={circle,draw, text width =1em,text centered,edge=->}}}
\begin{adjustbox}{valign=m}
\begin{forest}
circles
[{$f$}
[{$e$}]
[{$f$}
[{$x$}]
[{$i$}
[{$x$}]
]
]
]
\end{forest}
\end{adjustbox}
答案1
不清楚您所说的“装饰”是指图像的哪些方面。我假设是节点左上方/右上方的标签。代码中带有注释,解释了如何增加级别和兄弟节点之间的距离、如何使节点大小统一以及如何在相关位置添加标签。
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
circles/.style={
for tree={
math content,
circle,
draw,
text width=1em,
text centered,
edge=->,
s sep'+=5pt, % increase distance between siblings by 5pt
l sep'+=5pt, % increase distance between levels by 5pt
},
before typesetting nodes={
for tree={% make circles uniform in size
content/.wrap value=\strut ##1,
% split content of nodes into content and value for my label
split option={content}{:}{content,my label},
},
},
},
my label/.style={% put the label left or right, depending on which child and level we have
label/.process={On=On=|? {n'}{1} {level}{0} {45:$#1$}{135:$#1$}},
},
}
\begin{document}
\begin{forest}
circles
[f:\epsilon
[e:1]
[f:2
[x:21]
[i:22
[x:221]
]
]
]
\end{forest}
\end{document}
如果你本质上是在数树(除了根节点),你甚至不需要指定标签。你可以用类似下面的代码实现相同的结果
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
circles/.style={
for tree={
math content,
circle,
draw,
text width=1em,
text centered,
edge=->,
s sep'+=5pt, % increase distance between siblings by 5pt
l sep'+=5pt, % increase distance between levels by 5pt
},
before typesetting nodes={
for tree={% make circles uniform in size
content/.wrap value=\strut ##1,
if level=0{
my label=\epsilon,
}{
temptoksa=,
for nodewalk={filter={reverse={current and ancestors}}{>On>{level}{0}}}{temptoksa+/.option=n},
my label/.register=temptoksa,
},
},
},
},
my label/.style={% put the label left or right, depending on which child and level we have
label/.process={On=On=|? {n'}{1} {level}{0} {45:$#1$}{135:$#1$}},
},
}
\begin{document}
\begin{forest}
circles
[f
[e]
[f
[x]
[i
[x]
]
]
]
\end{forest}
\end{document}
为了比较: