树形图被截断

树形图被截断

在此处输入图片描述

编译树形图代码后,树的右侧部分被截断了。我已经使用了 \centering 函数,但这并不能解决问题。我不知道该如何修复它。谢谢大家

\documentclass[a4paper,landscape] {article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,calc,shapes,backgrounds}
\usepackage{amsmath, amssymb}



\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
arn/.style = {treenode, circle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=4.1ex},
arnrec/.style = {treenode, rectangle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=7.5ex,minimum width=4.0ex, minimum height=4.0ex},
arnsmall/.style = {treenode, circle, black, font=\sffamily\bfseries, draw=black,
    fill=white, text width=1.5ex},
}

\begin{document}
    \centering
    \tiny %font size
\begin{tikzpicture}[shorten >=1pt,scale=0.85]
 \node [sibling distance=10mm,level distance = 0.5cm] [arn] {start} 
 child{[sibling distance=6.5cm,level distance = 3cm] node [arn] {249}
            child{ [sibling distance=2.0cm, level distance = 2cm] node [arn] {281}  
                child{ [sibling distance=2cm] node [arn] {290} 
                    child{[] node [arnrec] {z=312}
                        edge from parent node[below]
                        {$x_{34}=1$}
                    }   
                    child{[] node [arnrec] {z=301}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{21}=1$} %label
                } 
                child{ [sibling distance=2cm] node [arn] {301} 
                edge from parent node[above]
                {$x_{23}=1$} %label
                }
                child{ [sibling distance=3cm] node [arn] {312} 
                edge from parent node[above]
                {$x_{24}=1$} %label
                }
            edge from parent node[above left]
            {$x_{12}=1$} %label
            }   
            child{ [sibling distance=3cm,level distance = 2cm] node [arn] {249} 
                child{ [black, sibling distance=2cm] node [arn] {267} 
                    child{[] node [arnrec] {z=312}
                        edge from parent node[below]
                        {$x_{34}=1$}
                    }   
                    child{[] node [arnrec] {z=301}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{21}=1$} %label
                } 
                child{ [sibling distance=2cm] node [arn] {276} 
                    child{[] node [arnrec] {z=299}
                        edge from parent node[below]
                        {$x_{32}=1$}
                    }   
                    child{[] node [arnrec] {z=325}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{24}=1$} %label
                }
                child{ [black, sibling distance=3cm] node [arn] {289} 
                edge from parent node[above]
                {$x_{25}=1$} %label
                } 
            edge from parent node[above]
            {$x_{13}=1$} %label
            } 
            child{ [sibling distance=2cm, level distance=2cm] node [arn] {287} 
                child{ [sibling distance=2cm] node [arn] {290} 
                edge from parent node[above]
                {$x_{21}=1$} %label
                } 
                child{ [sibling distance=2cm] node [arn] {301} 
                    child{[] node [arnrec] {z=299}
                        edge from parent node[below]
                        {$x_{32}=1$}
                    }   
                    child{[] node [arnrec] {z=325}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{23}=1$} %label
                }
                child{ [sibling distance=3cm] node [arn] {312} 
                edge from parent node[above]
                {$x_{24}=1$} %label
                }
            edge from parent node[above]
            {$x_{14}=1$} %label
            }
            child{ [sibling distance=3cm, level distance = 2cm] node [arn] {278} 
                child{ [sibling distance=2cm] node [arn] {296} 
                    child{[] node [arnrec] {z=312}
                        edge from parent node[below]
                        {$x_{34}=1$}
                    }   
                    child{[] node [arnrec] {z=301}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{21}=1$} %label
                } 
                child{ [sibling distance=2cm] node [arn] {294} 
                    child{[] node [arnrec] {z=299}
                        edge from parent node[below]
                        {$x_{32}=1$}
                    }   
                    child{[] node [arnrec] {z=325}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{23}=1$} %label
                }
                child{ [sibling distance=2cm] node [arn] {298} 
                    child{[] node [arnrec] {z=299}
                        edge from parent node[below]
                        {$x_{32}=1$}
                    }   
                    child{[] node [arnrec] {z=325}
                        edge from parent node[below]
                        {$x_{35}=1$}
                    }
                edge from parent node[above]
                {$x_{24}=1$} %label
                }
            edge from parent node[above right]
            {$x_{15}=1$} %label
            }                      
    edge from parent node[above right]
    {} %label
    }
;
\end{tikzpicture}
\end{document}

答案1

如果你只对结果输出感兴趣,可以使用standalone班级

\documentclass[margin=5pt]{standalone}

它将创建一个足够大小的页面,留下边距/边框5pt

在此处输入图片描述

答案2

forest除了 之外,我还可以推荐其他东西吗standalone?它允许您更紧凑地指定树,而无需过多减小字体大小。(我\tiny仅将其用于边缘标签和\small节点。)此外,您可以自动放置标签并使其更易读,并减少显式标记形式的重复量。

在以下示例中

  • my edge label={<label text>}用于指定自动放置的边缘标签。如果您愿意,可以进一步自动化此操作。
  • z={<value>}用于创建一个“z=”终端节点,自动改变形状并按照所需的格式排版值。

代码:

\documentclass[tikz,border=5pt,multi] {standalone}
\usepackage[utf8]{inputenc}
\usepackage{forest}

\begin{document}
\begin{forest}
  my edge label/.style={
    if={greater(n(),n_children("!u")/2)}{
      if={equal(n(),int((n_children("!u")+1)/2))}{
        edge label={node [pos=.6] {#1}},
      }{
        edge label={node [pos=.4, right] {#1}},
      },
    }{
      edge label={node [pos=.4, left] {#1}}
    },
  },
  z/.style={
    content={z=#1},
    rectangle
  },
  for tree={
    inner sep=1pt,
    text centered,
    align=center,
    font=\sffamily\bfseries\small,
    draw,
    circle,
    edge={font=\tiny},
    if n children=3{calign=child, calign child=2}{},
    s sep+=5pt,
    l sep+=15pt,
  }
  [start
    [249
      [281, my edge label={$x_{12}=1$}
        [290, my edge label={$x_{21}=1$}
          [, z=312, my edge label={$x_{34}=1$}]
          [, z=301, my edge label={$x_{35}=1$}]
        ]
        [301, my edge label={$x_{23}=1$}
        ]
        [312, my edge label={$x_{24}=1$}
        ]
      ]
      [249, my edge label={$x_{13}=1$}
        [267, my edge label={$x_{21}=1$}
          [, z=312, my edge label={$x_{34}=1$}]
          [, z=301, my edge label={$x_{35}=1$}]
        ]
        [276, my edge label={$x_{24}=1$}
          [, z=299, my edge label={$x_{32}=1$}]
          [, z=325, my edge label={$x_{35}=1$}]
        ]
        [289, my edge label={$x_{25}=1$}
        ]
      ]
      [287, my edge label={$x_{14}=1$}
        [290, my edge label={$x_{21}=1$}
        ]
        [301, my edge label={$x_{23}=1$}
          [, z=299, my edge label={$x_{32}=1$}]
          [, z=325, my edge label={$x_{35}=1$}]
        ]
        [312, my edge label={$x_{24}=1$}
        ]
      ]
      [278, my edge label={$x_{15}=1$}
        [296, my edge label={$x_{21}=1$}
          [, z=312, my edge label={$x_{34}=1$}]
          [, z=301, my edge label={$x_{35}=1$}]
        ]
        [294, my edge label={$x_{23}=1$}
          [, z=299, my edge label={$x_{32}=1$}]
          [, z=325, my edge label={$x_{35}=1$}]
        ]
        [298, my edge label={$x_{24}=1$}
          [, z=299, my edge label={$x_{32}=1$}]
          [, z=325, my edge label={$x_{35}=1$}]
        ]
      ]
    ]
  ]
\end{forest}
\end{document}

z 树

相关内容