我正在尝试使用森林包绘制一些树。出于某种原因,树叶的大小会根据树有多少片树叶而不同(见图)。
我的代码如下:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{forest}
\begin{document}
\begin{figure}[t]
\centering
\begin{forest}
for tree={circle,draw}
[
[, edge label={node[midway,left] {0}}
[$S^e_0$,edge label={node[midway,left] {0}} ]
[$S^e_1$,edge label={node[midway,right] {1}} ]
]
[, edge label={node[midway,right] {1}}
[$S^e_2$,edge label={node[midway,left] {0}} ]
[$S^e_3$,edge label={node[midway,right] {1}} ]
]
]
\end{forest}
\begin{forest}
for tree={circle,draw}
[
[, edge label={node[midway,left] {0}}
[, edge label={node[midway,left] {0}}
[$S^{e+1}_0$, edge label={node[midway,left] {0}}]
[$S^{e+1}_1$, edge label={node[midway,right] {1}}]
]
[$S^{e+1}_2$, edge label={node[midway,right] {1}} ]
]
[, edge label={node[midway,right] {1}}
[$S^{e+1}_3$,edge label={node[midway,left] {0}} ]
[$S^{e+1}_4$,edge label={node[midway,right] {1}} ]
]
]
\end{forest}
\begin{forest}
for tree={circle,draw}
[
[, edge label={node[midway,left] {0}}
[$S^{e+2}_0$,edge label={node[midway,left] {0}} ]
[$S^{e+2}_1$,edge label={node[midway,right] {1}} ]
]
[$S^{e+2}_2$, edge label={node[midway,right] {1}} ]
]
\end{forest}
\caption{...} \label{fig:...}
\end{figure}
\end{document}
答案1
具有适当的值minimum size
:
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{figure}[t]
\centering
\begin{forest}
for tree={circle,draw}
[
[, edge label={node[midway,left] {0}}
[$S^e_0$,minimum size=3em,edge label={node[midway,left] {0}} ]
[$S^e_1$,minimum size=3em,edge label={node[midway,right] {1}} ]
]
[, edge label={node[midway,right] {1}}
[$S^e_2$,minimum size=3em,edge label={node[midway,left] {0}} ]
[$S^e_3$,minimum size=3em,edge label={node[midway,right] {1}} ]
]
]
\end{forest}
\begin{forest}
for tree={circle,draw}
[
[, edge label={node[midway,left] {0}}
[, edge label={node[midway,left] {0}}
[$S^{e+1}_0$,minimum size=3em, edge label={node[midway,left] {0}}]
[$S^{e+1}_1$, minimum size=3em,edge label={node[midway,right] {1}}]
]
[$S^{e+1}_2$, minimum size=3em,edge label={node[midway,right] {1}} ]
]
[, edge label={node[midway,right] {1}}
[$S^{e+1}_3$,minimum size=3em,edge label={node[midway,left] {0}} ]
[$S^{e+1}_4$,minimum size=3em,edge label={node[midway,right] {1}} ]
]
]
\end{forest}
\begin{forest}
for tree={circle,draw}
[
[, edge label={node[midway,left] {0}}
[$S^{e+2}_0$,minimum size=3em,edge label={node[midway,left] {0}} ]
[$S^{e+2}_1$,minimum size=3em,edge label={node[midway,right] {1}} ]
]
[$S^{e+2}_2$, minimum size=3em,edge label={node[midway,right] {1}} ]
]
\end{forest}
\caption{...}
\label{fig:...}
\end{figure}
\end{document}
答案2
这也使用minimum width
类似leandriis 的回答但将其附加到所有没有子节点的节点。通过定义一种在所有树中使用的样式,可以实现进一步的简化。
\documentclass{article}
\usepackage{forest}
\forestset{my tree/.style={for tree={circle,
draw,
where n children=0{minimum width=3em}{}}}}
\begin{document}
\begin{figure}[t]
\centering
\begin{forest}
my tree
[
[, edge label={node[midway,left] {0}}
[$S^e_0$,edge label={node[midway,left] {0}} ]
[$S^e_1$,edge label={node[midway,right] {1}} ]
]
[, edge label={node[midway,right] {1}}
[$S^e_2$,edge label={node[midway,left] {0}} ]
[$S^e_3$,edge label={node[midway,right] {1}} ]
]
]
\end{forest}
\begin{forest}
my tree
[
[, edge label={node[midway,left] {0}}
[, edge label={node[midway,left] {0}}
[$S^{e+1}_0$, edge label={node[midway,left] {0}}]
[$S^{e+1}_1$, edge label={node[midway,right] {1}}]
]
[$S^{e+1}_2$, edge label={node[midway,right] {1}} ]
]
[, edge label={node[midway,right] {1}}
[$S^{e+1}_3$,edge label={node[midway,left] {0}} ]
[$S^{e+1}_4$,edge label={node[midway,right] {1}} ]
]
]
\end{forest}
~
\begin{forest}
my tree
[
[, edge label={node[midway,left] {0}}
[$S^{e+2}_0$,edge label={node[midway,left] {0}} ]
[$S^{e+2}_1$,edge label={node[midway,right] {1}} ]
]
[$S^{e+2}_2$, edge label={node[midway,right] {1}} ]
]
\end{forest}
\caption{...}
\label{fig:...}
\end{figure}
\end{document}
$
您可以使用来消除添加符号的需要math content
,但在这个例子中,我个人会发现这会令人困惑。
答案3
基于我的答案,简化了边标签的书写,并对树进行了稍微的重新设计:
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{figure}[t]
\tikzset{Circle/.style = {circle, draw, minimum size=8pt, inner sep=0pt}}
\forestset{
EL/.style 2 args={% shortens for (my) edge label
edge label={node[midway, font=\footnotesize, #1]{#2}},
},
}% end of forestset
\centering
\begin{forest}
for tree={circle, draw,
minimum size=2em, % <-- added
inner sep=1pt} % <-- added
[,Circle
[, Circle, EL={left}{0}
[$S^e_0$, EL={left}{0} ]
[$S^e_1$, EL={right}{1}]
]
[, Circle, EL={right}{1}
[$S^e_2$,EL={left}{0}]
[$S^e_3$,EL={right}{1}]
]
]
\end{forest}
\bigskip
\begin{forest}
for tree={circle, draw,
minimum size=2em, % <-- added
inner sep=1pt} % <-- added
[,Circle
[, Circle, EL={left}{0}
[, Circle, EL={left}{0}, fit=band
[$S^{e+1}_0$, EL={left}{0}]
[$S^{e+1}_1$, EL={right}{1}]
]
[$S^{e+1}_2$, EL={right}{1} ]
]
[, Circle, EL={right}{1}
[$S^{e+1}_3$, EL={left}{0}]
[$S^{e+1}_4$, EL={right}{1} ]
]
]
\end{forest}
\quad
\begin{forest}
for tree={circle, draw,
minimum size=2em, % <-- added
inner sep=1pt} % <-- added
[,Circle
[, Circle, EL={left}{0}, fit=band
[$S^{e+2}_0$, EL={left}{0} ]
[$S^{e+2}_1$, EL={right}{1} ]
]
[$S^{e+2}_2$, EL={right}{1} ]
]
\end{forest}
\caption{...}
\label{fig:...}
\end{figure}
\end{document}