我需要帮助来完成我的游戏树forest
。我已经成功地用连续的移动表示了树的不同级别和基本结构,请参阅代码。但是,我现在想在树中另外表示后向归纳法。我为此使用的方法是父子连接中的对角线。根据子级是在右侧还是左侧,对角线看起来会有所不同,如图所示(手工添加)。理想情况下,这些笔触应该是红色的,这样更容易识别。
\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{amsmath}
\usepackage{forest}
\usetikzlibrary{angles}
\usetikzlibrary{decorations.pathreplacing,calligraphy,backgrounds, through, calc}
\usetikzlibrary{through,calc}
%---------------------------------------------------------
\forestset{%
auto edge label/.style={%
before typesetting nodes={%
if level=0{}{
if={n()>(n_children("!u")/2)}{
if={n()==((n_children("!u")+1)/2)}{
edge label/.wrap value={
node[midway, right] {$##1$}
},
}{
edge label/.wrap value={
node[midway, outer sep=1.5mm, right] {$##1$}
},
},
}{
edge label/.wrap value={
node[midway, outer sep=1.5mm, left] {$##1$}
},
}
},
},
},
nice empty nodes/.style={% page 65 of the manual - this is from the linguistics library
for tree={
calign=fixed edge angles
},
delay={
where content={}{
shape=coordinate,
for parent={
for children={anchor=north}
}
}{}
}
},
arc below/.style={
tikz+={%
\begin{scope}
\clip (.center) coordinate (o) -- (!1.north) coordinate (a) |- (!2.north) coordinate (b) -| (!3.north) coordinate (c) -- cycle;
\node [draw, circle through={(b)}] at (o) {};
\draw [\forestoption{edge}] () -- ($(o)!1!-35:(b)$) ($(o)!1!35:(b)$) -- ();
\end{scope}
},
for children={
if n=2{}{no edge},
}
}
}
\tikzset{%
my circle/.style={draw, circle}
}
\begin{document}
\begin{forest}
for tree={
l sep=2em,
s sep=4em,
auto edge label,
nice empty nodes,
math content
}
[A, my circle, arc below
[, coordinate, edge label={0}]
[B, my circle, edge label={}
[{C}, my circle, edge label=0, arc below,
[, coordinate , edge label={0}]
[D, my circle, edge label={}
[E, my circle, edge label={0}
[{0}, edge label={0}]
[{0}, edge label={0}]
]
[{0}, edge label={0}]
]
[ , coordinate, edge label={0}]
]
[{0}, edge label={0}]
]
[, coordinate, edge label={0}]
]
\end{forest}
\end{document}
答案1
对于那些对istgame
包裹:
\documentclass{standalone}
\usepackage{istgame}
\begin{document}
\begin{istgame}
\setistmathTF111
\setxtarrowtips<.4>{Bar}[thick]
\setistEllipseNodeStyle{2em}
\xtdistance{15mm}{20mm}
\cntmdistance{20mm}{30mm}
\cntmAistb{0}[l]{0}[r]
\istrootocntmA(0){A} \istbA \endist
\istrooto(1)([yshift=-1em]0-1){B}
\istb[->-]{0}[l] \istb[->-]{0}[r]{0} \endist
\cntmAistb{0}[l]{0}[r]
\istrootocntmA(2)(1-1){C} \istbA \endist
\istrooto(3)([yshift=-1em]2-1){D}
\istb[->-]{0}[l] \istb{0}[r]{0} \endist
\istrooto(4)(3-1){E}
\istb[->-]{0}[l]{0} \istb{0}[r]{0} \endist
\end{istgame}
\end{document}
答案2
edge label
一般来说,通过设置为或类似设置来添加笔画是相当容易的node[midway]{/}
。然而,在 OP 的情况下,这行不通,因为它auto edge label
篡夺了edge label
选项(该选项仅存储数字,在排版节点之前将其转换为节点规范)。
解决方案是重新定义边编号样式以立即添加node[<options>]{<number>}
到edge label
。在下面的新代码中,auto edge label
应在各个节点处调用 。它执行edge label+={node[<options>]{#1}
,其中#1
是给定的边标签;与之前一样,<options>
根据子节点的位置进行定制。
现在我们可以类似地定义edge label stroke
向中添加一个包含适当笔画符号的节点edge label
。
请注意,auto edge label
和都edge label stroke
使用edge label+
加号,以添加到边缘标签。这样,可以按任意顺序使用这两种样式。
顺便说一句,由于和都auto edge label
需要edge label stroke
对子项的位置做出反应(在父项的左侧、正下方或右侧),我将这种逻辑概括为风格switch left middle right child
。
\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{amsmath}
\usepackage{forest}
\usetikzlibrary{angles}
\usetikzlibrary{decorations.pathreplacing,calligraphy,backgrounds, through, calc}
\usetikzlibrary{through,calc}
%---------------------------------------------------------
\forestset{%
% Execute keys #1/#2/#3 depending on whether this child is
% on the left of / just below / or on the right of the parent.
switch left middle right child/.style n args=3{
if={n()>(n_children("!u")/2)}{
if={n()==((n_children("!u")+1)/2)}{#2}{#3}
}{#1}
},
% ADD the number to the edge label
auto edge label/.style={%
switch left middle right child=
{edge label+={node[midway, outer sep=1.5mm, left]{$#1$}}}
{edge label+={node[midway, right ]{$#1$}}}
{edge label+={node[midway, outer sep=1.5mm, right]{$#1$}}}
},
% ADD the stroke to the edge label
edge label stroke/.style={
switch left middle right child=
{edge label+={node[midway,text=red,font=\bf]{\textbackslash}}}
{edge label+={node[midway,text=red,font=\bf]{---}}}
{edge label+={node[midway,text=red,font=\bf]{/}}}
},
nice empty nodes/.style={% page 65 of the manual - this is from the linguistics library
for tree={
calign=fixed edge angles
},
delay={
where content={}{
shape=coordinate,
for parent={
for children={anchor=north}
}
}{}
}
},
arc below/.style={
tikz+={%
\begin{scope}
\clip (.center) coordinate (o) -- (!1.north) coordinate (a) |- (!2.north) coordinate (b) -| (!3.north) coordinate (c) -- cycle;
\node [draw, circle through={(b)}] at (o) {};
\draw [\forestoption{edge}] () -- ($(o)!1!-35:(b)$) ($(o)!1!35:(b)$) -- ();
\end{scope}
},
for children={
if n=2{}{no edge},
}
}
}
\tikzset{%
my circle/.style={draw, circle}
}
\begin{document}
\begin{forest}
for tree={
l sep=2em,
s sep=4em,
nice empty nodes,
math content
}
[A, my circle, arc below
[, coordinate, auto edge label=0]
[B, my circle,
[{C}, my circle, auto edge label=0, arc below, edge label stroke,
[, coordinate , auto edge label=0]
[D, my circle,
[E, my circle, auto edge label=0, edge label stroke
[0, auto edge label=0, edge label stroke]
[0, auto edge label=0]
]
[0, auto edge label=0, edge label stroke]
]
[ , coordinate, auto edge label=0]
]
[0, auto edge label=0, edge label stroke]
]
[, coordinate, auto edge label=0]
]
\end{forest}
\end{document}