![用不同的角度和平行线重现森林](https://linux22.com/image/474190/%E7%94%A8%E4%B8%8D%E5%90%8C%E7%9A%84%E8%A7%92%E5%BA%A6%E5%92%8C%E5%B9%B3%E8%A1%8C%E7%BA%BF%E9%87%8D%E7%8E%B0%E6%A3%AE%E6%9E%97.png)
我已经使用了代码成本加运费精美生成这里重新创建下图:
我的努力的 MWE 如下:
\documentclass[11pt]{book}
\usepackage{graphicx}
\usepackage{amsthm,amssymb}
\usepackage{unicode-math}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{forest}
\begin{document}
\begin{figure}[h]
\centering
\begin{forest}
for tree={
circle,
fill=black,
calign=fixed edge angles,
inner sep=0.5pt,
},
before typesetting nodes={
for tree={%
my label/.option=content,
content={.},
}
},
my label/.style={%
label/.process={ O+nn= ? p w1 {level}{0} {_{below}} {On= ? {n}{1} {left}{right}} {##1:{$#1$}} },
% if level=0{label=below:{$#1$}}{if n=1{label=left:{$#1$}}{label=right:{$#1$}}},
},
[
[,edge label={node(y1)[midway,left,font=\scriptsize]{1}}
[7,name=chap,edge label={node(y2)[midway,left,font=\scriptsize]{1}}
[,phantom]
[6,edge label={node [midway,right,font=\scriptsize]{0}}
[,phantom]
[5,edge label={node [midway,right,font=\scriptsize]{0}}]
]
]
[6,edge label={node [midway,right,font=\scriptsize]{0}}
[,phantom]
[,edge label={node [midway,right,font=\scriptsize]{0}}
[5,edge label={node [midway,left,font=\scriptsize]{1}}]
[,phantom]
]
]
]
[,edge label={node[midway,right,font=\scriptsize]{0}}
[,edge label={node[midway,left,font=\scriptsize]{1}}]
[,edge label={node[midway,right,font=\scriptsize]{0}}
[,name=y4,edge label={node(y3)[midway,left,font=\scriptsize]{1}}]
[,phantom]
]
]
]
\end{forest}
\end{figure}
\end{document}
正如您所看到的,除了不完整(由于空间不足,我无法绘制图表的其余部分)之外,它还有两个问题:
- 标签“6”和“5”的位置错误。
- 边缘之间的角度太锐利。
您能否帮助我定义样式,以便最终的图形类似于上面显示的所需图形?
答案1
您可以通过设置 来实现这一点calign=fixed edge angles
,然后指定每对子项的角度(角度以 0 为垂直方向,左侧为负,右侧为正)。您必须添加一些phantom
子项才能使其工作。请注意,calign angle=θ
是 的缩写:calign primary angle=-θ, calign secondary angle=θ
。
\documentclass{article}
\usepackage{forest}
\tikzset{dot/.style={fill, circle, inner sep=0pt, minimum size=5pt}}
\begin{document}
\begin{forest}
for tree={dot, calign=fixed edge angles, l sep=1cm}
[, calign angle=60
[, calign primary angle=-60, calign secondary angle=30
[, label=left:7, calign angle=30
[, phantom][, label=left:6, calign angle=30[, phantom][, label=left:5]]
]
[, label=left:6, calign angle=30
[, phantom][, calign angle=30[, phantom][, label=left:5]]
]
]
[, calign primary angle=-30, calign secondary angle=60
[, label=left:6
[, phantom][, calign angle=30, label=below:$5\le\mathit{PROF}$]
]
[, calign primary angle=-30, calign secondary angle=60
[, label=left:6, calign angle=30[, phantom][, label=left:6]]
[, calign angle=30[][, phantom]]
]
]
]
\end{forest}
\end{document}
答案2
下面的代码可以让你实现或多或少的自动布局,但代价是很多的空间。它形成一棵腿很长的树。phantom
仍然需要插入 以达到塑形的目的,但会自动添加以抵消层级对齐和 之间的冲突calign=fixed angles
。
\documentclass[11pt]{standalone}
% ateb: https://tex.stackexchange.com/a/711690/ addaswyd o/i gwestiwn Arian: https://tex.stackexchange.com/q/711587/
% atebion: https://tex.stackexchange.com/a/703530/ a https://tex.stackexchange.com/a/703351/ i gwestiwn Arian: https://tex.stackexchange.com/q/703321/
\usepackage{amsmath}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{%
my label/.style={%
label/.process={ O+nn= ? p w1 {n children}{0} {_{below}} {On= ? {n}{1} {left}{right}} {##1:{$#1$}} },
},
align tree B/.style={%
for tree={
circle,
fill=black,
calign=fixed angles,
inner sep=0.5pt,
tier/.option=level,
},
delay={
where phantom={}{%
my label/.option=content,
content={.},
},
},
spread align to tier,
},
my phantom/.style={%
phantom,
tier/.option=level,
},
spread align to tier/.style={%
before typesetting nodes={%
tempcounta/.max={level}{tree},
not tempboola,
until={>R{tempboola}}{%
tempboola,
for tree breadth-first={%
if={ > OR< On= & {level}{tempcounta} {n children}{0} }{%
repeat=2{append={[,my phantom]}},
not tempboola,
}{},
},
do dynamics,
},
},
},
}
\begin{document}
\begin{forest}
align tree B,
for tree={%
math content,
calign angle=50,
},
where n=1{%
edge label={node [midway,left,font=\scriptsize]{1}},
}{
edge label={node [midway,right,font=\scriptsize]{0}},
},
[
[
[7
[,phantom]
[6
[,phantom]
[5]
]
]
[6
[,phantom]
[
[5]
[,phantom]
]
]
]
[
[
[,phantom]
[5\leq\textsc{prof}]
]
[
[6
[,phantom]
[6]
]
[, phantom
]
]
]
]
\end{forest}
\end{document}
森林的布局算法不能以人们所希望的优雅或高雅来处理这种树!