按照包文档中的示例forest
,我制作了以下决策树:
\documentclass{article}
\usepackage{mathtools,forest}
\def\getfirst#1;#2\endget{#1}
\def\getsecond#1;#2\endget{#2}
\forestset{declare toks={elo}{}} % edge label options
\begin{document}
\color{red}
\begin{forest}
%=============================================
anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
for tree={font=\footnotesize,
s sep=5mm,l=15mm,
if n children=0{anchors=north}{
if n=1{anchors=south east}{anchors=south west}},
content format={$\forestoption{content}$}
},
anchors=south, outer sep=2pt,
nomath/.style={content format=\forestoption{content}},
dot/.style={tikz+={\draw[#1](.child anchor)circle[radius=2pt];}},
dot={fill={white}},for descendants={dot={fill}}, % initial node hollow, rest solid
decision edge label/.style n args=3{
edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$#3$}}
},
decision/.style={if n=1
{decision edge label={left}{east}{#1}}
{decision edge label={right}{west}{#1}}
},
delay={for descendants={
decision/.expanded/.wrap pgfmath arg={\getsecond#1\endget}{content},
content/.expanded/.wrap pgfmath arg={\getfirst#1\endget}{content},
}},
%=============================================
[Name,nomath
[Name;{+\epsilon},nomath
[;{H}]
[;{T}]
]
[Name;{-\epsilon},nomath
[;{H}]
[;{T}]
]
]
\end{forest}
\end{document}
虽然树的语法相对简单,但准备工作——定义样式/键/选项(两===
行之间的内容)——却相当多。
如果我想在文档中绘制多个这样的树,并且样式/键/选项设置几乎相同,有没有办法在序言中“预定义”这些树?我尝试将设置复制到命令中\forestset{mytree/.style={...}}
,但 TeX 告诉我参数数量错误。如果我使用\forestset{mytree/.style n args=3{...}}
,我不知道 在环境mytree
中使用时要给出什么参数forest
。
答案1
您可以创建一个样式,用于树的序言中。这很简单:
\forestset{
decision tree/.style={
<stuff from preamble>
}
}
唯一要记住的不是具体到森林那就是在定义样式时,你需要使用双斜杠符号来获取相关参数。否则,TeX 会将参数传递给风格而不是什么风格定义。
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{mathtools,forest}
\def\getfirst#1;#2\endget{#1}
\def\getsecond#1;#2\endget{#2}
\forestset{declare toks={elo}{}}
\forestset{
decision tree/.style={
anchors/.style={
anchor=##1,
child anchor=##1,
parent anchor=##1
},
for tree={
font=\footnotesize,
s sep=5mm,
l=15mm,
if n children=0{anchors=north}{
if n=1{anchors=south east}{anchors=south west}
},
content format={$\forestoption{content}$}
},
anchors=south,
outer sep=2pt,
nomath/.style={content format=\forestoption{content}},
dot/.style={
tikz+={
\draw[##1](.child anchor)circle[radius=2pt];
}
},
dot={fill={white}},
for descendants={dot={fill}},
decision/.style={
if n=1{
decision edge label={left}{east}{##1}
}{
decision edge label={right}{west}{##1}
}
},
decision edge label/.style n args=3{
edge label/.expanded={node[midway,auto=##1,anchor=##2,\forestoption{elo}]{\strut$##3$}}
},
delay={
for descendants={
decision/.expanded/.wrap pgfmath arg={\getsecond##1\endget}{content},
content/.expanded/.wrap pgfmath arg={\getfirst##1\endget}{content},
}
},
}
}
\begin{document}
\color{red}
\begin{forest}
decision tree,
[Name,nomath
[Name;{+\epsilon},nomath
[;{H}]
[;{T}]
]
[Name;{-\epsilon},nomath
[;{H}]
[;{T}]
]
]
\end{forest}
\color{blue}
\begin{forest}
decision tree,
[Name,nomath
[Name;{+\epsilon},nomath
[;{H}]
[;{T}]
]
[Name;{-\epsilon},nomath
[;{H}]
[;{T}]
]
]
\end{forest}
\end{document}