以下 MWE 生成了forest
我想要的树形图(使用包):
\documentclass[border=3mm]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{ELS/.style={% Edge Label Style
font=\footnotesize, inner sep=2pt,
anchor=south #1, % label position: "ELS=west" or "EL=east"
pos=0.6}
}
\newcommand\EL{edge label}
\begin{forest}
for tree={
draw, semithick,
parent anchor=children,
l sep=13 mm,
s sep= 1 mm,
anchor=parent,% <--- work around alignment issue
},
EL/.style = {
before typesetting nodes={%% <--- work around edge labels positioning
where n=1{%
edge label/.wrap value={node[ELS=east]{$#1$}}
}{%
edge label/.wrap value={node[ELS=west]{$#1$}}
}
}
}
[Start
[A, EL=\leq 0.5
[A1, EL=\leq 0.3
[A11, EL=\leq 0.7]
[A12, EL=\geq 0.7]
]
[,phantom,fit=band] % <---
[A2, EL=\geq 0.3
[A21, EL=\leq 0.7]
[A22, EL=\geq 0.7]
]
]
[,phantom,fit=band] % <---
[B, EL=\geq 0.5
[B1, EL=\leq 0.3
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
[,phantom,fit=band] % <---
[B2, EL=\geq 0.3
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
]
]
\end{forest}
\end{document}
我想知道,是否有可能在不插入幻像节点的情况下获得相同的结果?此外,我将不胜感激对我的 MWE 代码的任何改进。
附录:一种方法是手动插入,s sep
如下面的代码所示:
[Start,s sep=4mm
[A, EL=\leq 0.5,s sep=4mm
[A1, EL=\leq 0.3,s sep=1mm
[A11, EL=\leq 0.7]
[A12, EL=\geq 0.7]
]
[A2, EL=\geq 0.3,s sep=1mm
[A21, EL=\leq 0.7]
[A22, EL=\geq 0.7]
]
]
[B, EL=\geq 0.5,s sep=4mm
[B1, EL=\leq 0.3,s sep=1mm
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
[B2, EL=\geq 0.3,s sep=1mm
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
]
]
考虑到上述解决方案,现在的问题是:是否可以在fortree
序言中编写一个代码,它将取代那些手动插入的代码s sep=...
?
答案1
是的。使用s sep+
。
\documentclass[border=3mm]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{ELS/.style={% Edge Label Style
font=\footnotesize, inner sep=2pt,
anchor=south #1, % label position: "ELS=west" or "EL=east"
pos=0.6}
}
\newcommand\EL{edge label}
\begin{forest}
for tree={
draw, semithick,
parent anchor=children,
l sep=13 mm,
s sep+=2mm,
anchor=parent,% <--- work around alignment issue
},
EL/.style = {
before typesetting nodes={%% <--- work around edge labels positioning
where n=1{%
edge label/.wrap value={node[ELS=east]{$#1$}}
}{%
edge label/.wrap value={node[ELS=west]{$#1$}}
}
}
}
[Start
[A, EL=\leq 0.5
[A1, EL=\leq 0.3,s sep+=-3mm
[A11, EL=\leq 0.7]
[A12, EL=\geq 0.7]
]
%[,phantom,fit=band] % <---
[A2, EL=\geq 0.3,s sep+=-3mm
[A21, EL=\leq 0.7]
[A22, EL=\geq 0.7]
]
]
%[,phantom,fit=band] % <---
[B, EL=\geq 0.5
[B1, EL=\leq 0.3,s sep+=-3mm
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
%[,phantom,fit=band] % <---
[B2, EL=\geq 0.3,s sep+=-3mm
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
]
]
\end{forest}
\end{document}
在 Zarko 给我发信息之前,我的电脑上就有这个。(我认为没有必要为它添加更新。)
\documentclass[border=3mm]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{ELS/.style={% Edge Label Style
font=\footnotesize, inner sep=2pt,
anchor=south #1, % label position: "ELS=west" or "EL=east"
pos=0.6}
}
\newcommand\EL{edge label}
\begin{forest}
for tree={
draw, semithick,
parent anchor=children,
s sep=4mm,
l sep=13 mm,
anchor=parent,% <--- work around alignment issue
},
EL/.style = {
before typesetting nodes={%% <--- work around edge labels positioning
where n=1{%
edge label/.wrap value={node[ELS=east]{$#1$}}
}{%
edge label/.wrap value={node[ELS=west]{$#1$}}
},
where level=2{s sep+=-1mm}{s sep+=0.5mm}
}
}
[Start
[A, EL=\leq 0.5
[A1, EL=\leq 0.3
[A11, EL=\leq 0.7]
[A12, EL=\geq 0.7]
]
%[,phantom,fit=band] % <---
[A2, EL=\geq 0.3
[A21, EL=\leq 0.7]
[A22, EL=\geq 0.7]
]
]
%[,phantom,fit=band] % <---
[B, EL=\geq 0.5
[B1, EL=\leq 0.3
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
%[,phantom,fit=band] % <---
[B2, EL=\geq 0.3
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
]
]
\end{forest}
\end{document}
答案2
仅供记录。在 @marmot 的精彩回答的帮助下,在 forest
反复阅读文档后,我找到了我所寻找的解决方案:
\documentclass[border=3mm, preview]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{ELS/.style={% Edge Label Style
font=\footnotesize\sffamily, inner sep=2pt,
anchor=south #1, % label position: "ELS=west" or "ELS=east"
pos=0.6}
}
\begin{forest}
for tree={
draw,
semithick,
% fill=orange!20,
parent anchor=children,
l sep=13 mm,
s sep=4mm,
where level=2{s sep=1mm}{}
},
EL/.style = {
before typesetting nodes={%
where n=1{%
edge label/.wrap value={node[ELS=east]{$#1$}}
}{%
edge label/.wrap value={node[ELS=west]{$#1$}}
}
}
}
[Start
[A, EL=\leq 0.5
[A1, EL=\leq 0.3
[A11, EL=\leq 0.7]
[A12, EL=\geq 0.7]
]
[A2, EL=\geq 0.3
[A21, EL=\leq 0.7]
[A22, EL=\geq 0.7]
]
]
[B, EL=\geq 0.5
[B1, EL=\leq 0.3
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
[B2, EL=\geq 0.3
[B11, EL=\leq 0.7]
[B12, EL=\geq 0.7]
]
]
] \end{forest}
\end{document}
得到期望的结果: