
我正在使用 TikZ 繪製分支定界樹,我需要一些幫助。
這是我的程式碼:
\documentclass[
12pt,
oneside,
a4paper,
english,
french,
spanish,
brazil
]{abntex2}
\usepackage{graphicx}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{calc, shapes}
\begin{document}
\begin{figure}[H]
\centering
\tikzset{thick,
tree node/.style = {align=center, inner sep=0pt, font = \scriptsize},
every label/.append style = {font=\scriptsize},
S/.style = {draw, circle, minimum size = 20mm, inner sep=0pt,
top color=white, bottom color=blue!20},
ENL/.style = {% edge node left
font=\footnotesize, left=1pt},
ENR/.style = {% edge node right
font=\footnotesize, right=1pt},
grow = down,
sibling distance = 2.8cm,
level distance = 3cm
}
\begin{tikzpicture}
\node [S, label= 2:Some text here] {$PL0$}
child{node [S] {$PL1$}
child{node [S] {$PL3$}
edge from parent node[ENL] {$x2 \leq 2$}}
child{node [S] {$PL4$}
child{node [S] {$PL5$}
child{node [S] {$PL7$}
edge from parent node[ENL] {$x2 \leq 3$}}
child{node [S, label = 2:More text here] {$PL8$}
edge from parent node[ENR] {$x2 \geq 4$}}
edge from parent node[ENL] {$x1 \leq 3$}}
child{node [S, label = 2:Another text here] {$PL6$}
edge from parent node[ENR] {$x1 \geq 4$}}
edge from parent node[ENR] {$x2 \geq 3$}}
edge from parent node[ENL] {$x1 \leq 4$}}
child{node [S, label = 2:Last text here] {$PL2$}
edge from parent node[ENR] {$x1 \geq 5$}};
\end{tikzpicture}
\caption{My Figure.}
\end{figure}
\end{document}
結果,您應該得到這個數字:
嗯,我需要幫忙解決兩件事:
即使使用該
\centering
命令,圖形也不居中。我在這裡缺少什麼?我需要在這些圈子內寫更多資訊。例如,在第一個 (PL0) 中,它應該具有以下附加資訊:
我嘗試了很多東西,但無法使這些資訊符合圓圈。
答案1
- 圖是居中。為了驗證這一點,我在文字區域和
tikzpicture
. - 如果您想在圈子中添加更多內容,則需要將其添加到節點內容中。如果要允許多行文本,則需要添加適當的對齊(或
text width
)鍵,例如align=center
。
\documentclass[
12pt,
oneside,
a4paper,
english,
french,
spanish,
brazil
]{abntex2}
\usepackage[showframe]{geometry}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{calc, shapes}
\begin{document}
\begin{figure}[H]
\centering
\tikzset{thick,
tree node/.style = {align=center, inner sep=0pt, font = \scriptsize},
every label/.append style = {font=\scriptsize},
S/.style = {draw, circle, minimum size = 20mm, inner sep=0pt,
top color=white, bottom color=blue!20,
align=center}, %<-
ENL/.style = {% edge node left
font=\footnotesize, left=1pt},
ENR/.style = {% edge node right
font=\footnotesize, right=1pt},
grow = down,
sibling distance = 2.8cm,
level distance = 3cm
}
\begin{tikzpicture}
\node [S, label= 2:Some text here] {$PL0$}
child{node [S] {$PL0$\\ $x_1 = 4,333$ e\\ $x_2 = 2$ \\ $f = 42$}
child{node [S] {$PL3$}
edge from parent node[ENL] {$x2 \leq 2$}}
child{node [S] {$PL4$}
child{node [S] {$PL5$}
child{node [S] {$PL7$}
edge from parent node[ENL] {$x2 \leq 3$}}
child{node [S, label = 2:More text here] {$PL8$}
edge from parent node[ENR] {$x2 \geq 4$}}
edge from parent node[ENL] {$x1 \leq 3$}}
child{node [S, label = 2:Another text here] {$PL6$}
edge from parent node[ENR] {$x1 \geq 4$}}
edge from parent node[ENR] {$x2 \geq 3$}}
edge from parent node[ENL] {$x1 \leq 4$}}
child{node [S, label = 2:Last text here] {$PL2$}
edge from parent node[ENR] {$x1 \geq 5$}};
\draw (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\caption{My Figure.}
\end{figure}
\end{document}
我還建議您查看forest
包裝。