我正在使用 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
包装。