3

enter image description here

After compiling the code for a tree diagram, some parts of the right hand side of the tree got cut off. I've already used \centering function but this does not fix the problem.I'm not sure how to fix it. Thanks guys

\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}
  • You either have to enlarge the paper to make the image fit, or you have to shrink the image down to fit the page. Alternatively, you could use the standalone document class and it'll make sure the image fits... on a page the clips to the size of the image. What do you want exactly? Do you want to maintain the landscape, a4paper size? – Werner Apr 8 '15 at 20:48
  • @Werner The page orientation doesn't really matter, neither is the paper size because I only need to capture(print screen) the whole part of the tree diagram. – alex12 Apr 8 '15 at 20:51
3

If you're just interested in the resulting output, use the standalone class:

\documentclass[margin=5pt]{standalone}

It will create a page of sufficient size leaving a margin/border of 5pt.

enter image description here

0

May I recommend forest in addition to standalone? It allows you to specify the tree more compactly, without reducing the font size quite so much. (I used \tiny only for the edge labels and \small for the nodes.) Moreover, you can automate the placement of labels and make them more readable, and reduce the amount of repetition in the form of explicit mark up.

In the following example

  • my edge label={<label text>} is used to specify the edge labels which are automatically placed. If you wished, you could further automate this.
  • z={<value>} is used to create one of the 'z=' terminal nodes, automatically changing the shape and typesetting the value in the required format.

Code:

\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 tree

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.