\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {A-B-B}
child {
node[bag] {B}
child {
node[end] {}
}
child {
node[end] {}
}
}
child {
node[bag] {A}
child {
node[bag] {B}
child {
node[bag] {B}
child {
node[bag] {B}
child {
node[bag] {B}
}
}
}
}
child {
node[end] {}
}
};
\end{tikzpicture}
\end{document}
答案1
您的图表太宽,无法适应文本宽度。要解决此问题,您有三种方法:
包含横向页面方向的图表
重新绘制图形,使其向下(下方)增长,而不是向右
使水平距离更小。
最后一种可能性的代码如下。在其中,我还稍微更改了节点选项并添加了选项(作为查看其边框的临时解决方案)draw
。在序言中还添加了showframe
显示页面布局的包。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{showframe}
\begin{document}
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=2cm, sibling distance=3cm]
\tikzstyle{level 2}=[sibling distance=2cm]
% Define styles for bags and leafs
\tikzstyle{bag} = [draw, minimum width=2em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {A-B-B}
child {
node[bag] {B}
child {
node[end] {}
}
child {
node[end] {}
}
}
child {
node[bag] {A}
child {
node[bag] {B}
child {
node[bag] {B}
child {
node[bag] {B}
child {
node[bag] {B}
}
}
}
}
child {
node[end] {}
}
};
\end{tikzpicture}
\end{document}
当然,在实际使用中您只需省略所有演示边框。