
答案1
這是定義森林樣式的版本,state tree
用於格式化這種類型的樹。基於該範例的假設是
- 兒童應均勻分佈;
- 第一個和最後一個子節點與父節點的距離應相等;
- 應該只為第一個和最後一個子元素繪製邊緣;
- 節點的內容應以數學模式排版。
如果這些假設中的一項或多項需要修改,則可以修改樣式。
這是樹的程式碼,稍作修改伊格納西的:
\begin{forest}
state tree,
[x_0
[x_1
[x_{11}
[x_{111}]
[\dot{\cup}]
[x_{112}]
]
[\times]
[x_{12}
[x_{121}]
[\dot{\cup}]
[x_{122}]
]
]
[\dot{\cup}]
[x_2]
[\dot{\cup}]
[x_3
[x_{31}]
[\dot{\cup}]
[x_{32}]
[\dot{\cup}]
[x_{33}]
]
]
\end{forest}
state tree
設定樣式。其餘的都是自動的,所以不需要no edge
等。
這會產生以下結果:
完整程式碼:
\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
% ateb: addaswyd o ateb Ignasi: https://tex.stackexchange.com/a/351690/
\forestset{%
state tree/.style={%
for tree={
math content,
parent anchor=children,
child anchor=parent,
tier/.option=level,
calign=center,
},
where={>On>{n children}{2}}{
for nodewalk={
filter={children}{>On=!On=!&{n}{1}{n'}{1}}
}{no edge}
}{},
before computing xy={
where={isodd(n_children)}{
tempdima/.process={OOOw3+d{!n=1.s}{!n'=1.s}{n children}{(##2-##1)/(##3-1)}},
tempdimb/.option={!n=1.s},
for children={
s/.process={RROw3+d{tempdima}{tempdimb}{n}{##2+(##1*(##3-1))}}
},
}{},
},
},
}
\begin{document}
\begin{forest}
state tree,
[x_0
[x_1
[x_{11}
[x_{111}]
[\dot{\cup}]
[x_{112}]
]
[\times]
[x_{12}
[x_{121}]
[\dot{\cup}]
[x_{122}]
]
]
[\dot{\cup}]
[x_2]
[\dot{\cup}]
[x_3
[x_{31}]
[\dot{\cup}]
[x_{32}]
[\dot{\cup}]
[x_{33}]
]
]
\end{forest}
\end{document}
答案2
你也可以嘗試使用forest
,儘管我不知道如何正確分配一些孩子:
\documentclass [multi=forest, border=2mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={math content}
[x_0
[x_1
[x_{11}
[x_{111}]
[\dot{\cup}, no edge]
[x_{112}]]
[\times, no edge]
[x_{12}
[x_{121}]
[\dot{\cup}, no edge]
[x_{122}]]]
[\dot{\cup}, no edge]
[x_2, no edge]
[\dot{\cup}, no edge]
[x_3
[x_{31}]
[\dot{\cup}, no edge]
[x_{32}, no edge]
[\dot{\cup}, no edge]
[x_{33}]]]
\end{forest}
\end{document}
更新:根據 JLDiaz 的建議,結果看起來更好,仍然不完美,但更好。
\documentclass [multi=forest, border=2mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={math content}
[x_0
[x_1
[x_{11}
[x_{111}]
[\dot{\cup}, no edge]
[x_{112}]]
[\null, no edge] %<-------------
[\times, no edge]
[x_{12}
[x_{121}]
[\dot{\cup}, no edge]
[x_{122}]]]
[\null, no edge] %<-------------
[\dot{\cup}, no edge]
[x_2, no edge]
[\dot{\cup}, no edge]
[x_3
[x_{31}]
[\dot{\cup}, no edge]
[x_{32}, no edge]
[\dot{\cup}, no edge]
[x_{33}]]]
\end{forest}
\end{document}
答案3
第一次嘗試如下。我在畫樹方面不太有經驗,所以可能有更好的方法來做到這一點。然而,關鍵點是在子節點之間放置節點,而該calc
庫非常適合於此:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\rootname{root}
\newcommand{\stnode}[3]{\node at ($(#1)!0.5!(#2)$) { #3 }}
\newcommand{\stnodec}[3]{\node at ($(#1.center)!0.5!(#2.center)$) { #3 }}
\newcommand{\stnoderootc}[3]{\node at ($(\rootname-#1.center)!0.5!(\rootname-#2.center)$) { #3 }}
\begin{document}
\begin{tikzpicture}
[
level 1/.style = {sibling distance = 3cm},
level 2/.style = {sibling distance = 2cm}
]
\node (root) { $x_0$ }
child { node { $x_1$ }
child { node { $x_3$ } }
child { node { $x_4$ } }
}
child { node { $x_2$ }
child { node { $x_5$ } }
child { node { $x_6$ } }
}
;
place the annotation nodes between the children
\stnode{root-1.center}{root-2.center}{$\times$};
\stnodec{root-1-1}{root-1-2}{$\times$};
\stnoderootc{2-1}{2-2}{$\times$};
\end{tikzpicture}
\end{document}
您可以使用三個巨集之一 ( \stnode
, \stnodec
(C輸入), \stnoderootc
(自動根節點,center))放置註解節點。必須重新定義根名稱以表示根節點的實際名稱。為了獲得最大的靈活性,只需使用\stnode
.
參數為#1:第一個節點,#2:第二個節點,#3:節點文字。
顯然,各個節點應該具有相同的 y 位置,如果不是這種情況,它仍然可以工作,但看起來會很奇怪。