3

My forest looks like this but it does not fit on a page, this is my first time using this package. I am looking for suggestions to fit this into a page, hopefully they will not be too intensive because I plan on making an even bigger tree.

\documentclass{article}
\usepackage{forest}

\begin{document}
\begin{forest}
for tree={circle,draw, l sep=20pt}
 [1,red 
[2, edge label={node[midway,left] {A}}
  [1,red,edge label={node[midway,left] {B}} 
            [2,red,edge label={node[midway,right] {C}}]
            [2,red,edge label={node[midway,right] {D}}]
        ] 
  [1,red,edge label={node[midway,right] {C}}
            [2,red,edge label={node[midway,right] {B}}]
            [2,red,edge label={node[midway,right] {D}}]
            ]
        [1,red,edge label={node[midway,right] {D}}
            [2,red,edge label={node[midway,right] {B}}]
            [2,red,edge label={node[midway,right] {C}}]
            ]
]
[2, edge label={node[midway,left] {B}}
  [1,red, edge label={node[midway,left] {A}}
            [2,red,edge label={node[midway,right] {C}}]
            [2,red,edge label={node[midway,right] {D}}]
            ] 
  [1,red, edge label={node[midway,right] {A}}
            [2,red,edge label={node[midway,right] {C}}]
            [2,red,edge label={node[midway,right] {D}}]
        ]
        [1,red,edge label={node[midway,right] {A}}
            [2,red,edge label={node[midway,right] {C}}]
            [2,red,edge label={node[midway,right] {D}}]
        ]
]
    [2, edge label={node[midway,left] {C}}  
  [1,red,edge label={node[midway,left] {A}} 
            [2,red,edge label={node[midway,right] {B}}]
            [2,red,edge label={node[midway,right] {D}}]
        ] 
  [1,red,edge label={node[midway,right] {B}} 
            [2,red,edge label={node[midway,right] {A}}]
            [2,red,edge label={node[midway,right] {D}}]
        ]
        [1,red,edge label={node[midway,right] {D}}
            [2,red,edge label={node[midway,right] {A}}]
            [2,red,edge label={node[midway,right] {B}}]
            ]
]
    [2, edge label={node[midway,left] {D}}
  [1,red,edge label={node[midway,left] {A}} 
            [2,red,edge label={node[midway,right] {B}}]
            [2,red,edge label={node[midway,right] {C}}]
            ] 
  [1,red,edge label={node[midway,right] {B}}
            [2,red,edge label={node[midway,right] {A}}]
            [2,red,edge label={node[midway,right] {C}}]
            ]
        [1,red,edge label={node[midway,right] {C}}
            [2,red,edge label={node[midway,right] {A}}]
            [2,red,edge label={node[midway,right] {B}}]
            ]
    ]
]
\end{forest}
\end{document}
  • 1
    Welcome! Please complete your code to make a compilable document. There are various options here, but without knowing how large your pages are, it is impossible to give a meaningful answer other than scale-the-tree-to-fit which is a solution to be avoided if at all possible. (I sometimes use it with Beamer, but it is not a good option at all.) Also, it would help to know what kind of solution would work. Since we don't know what the tree is about, we don't know if, for example, it is essential to keep nodes on a single level aligned or not. – cfr Sep 15 '16 at 15:25
  • I've completed the code. The tree will pretty much always be symmetric though I don't really mind if it appears asymmetric for fitting purposes. – Dio Sep 15 '16 at 16:57
  • Hi, if anybody is still looking at this, is there a way to output a 'page' as large as needed to display a large tree and to navigate it up and down (say, like a google map)? – mario Jun 29 '18 at 12:02
4

I would:

  • use geometry to get more sensible margins;
  • move some branches of the tree down to conserve space;
  • used squared edges to avoid branches crossing things and to reduce crowding and clutter;
  • avoid putting labels so that edges are drawn through them by repositioning them slightly for greater legibility;
  • use a style to simplify adding the edge labels, which allows their positions to be amended more easily and determined more consistently (and saves typing) e.g. my label in the example below;
  • consider adding colour automatically for trees where there is a pattern e.g. all final nodes are a different colour or all left-hand nodes or whatever (but this is just to save typing and clearly a matter of preference);
  • use pdflscape for larger trees (not needed for this one).

Here's an example:

rearranged tree

\documentclass{article}
\usepackage{geometry}
\usepackage[edges]{forest}

\begin{document}
\noindent
\begin{forest}
  my label/.style={%
    if n=1{%
      edge label={node [midway,left] {#1}}
    }{%
      if n'=1{%
        edge label={node [midway,right] {#1}}
      }{%
        edge label={node [midway,below right] {#1}}
      }
    },
  },
  for tree={circle,draw, l sep=20pt},
  before typesetting nodes={
    where content={}{coordinate}{},
  },
  forked edges,
 [1,red
  [2, my label={A}
    [1,red,my label={B}
              [2,red,my label={C}]
              [2,red,my label={D}]
          ]
    [1,red,my label={C}
              [2,red,my label={B}]
              [2,red,my label={D}]
              ]
          [1,red,my label={D}
              [2,red,my label={B}]
              [2,red,my label={C}, tier=this]
              ]
  ]
  [, tier=this, my label={B}
  [2
    [1,red, my label={A}
              [2,red,my label={C}]
              [2,red,my label={D}]
              ]
    [1,red, my label={A}
              [2,red,my label={C}]
              [2,red,my label={D}]
          ]
          [1,red,my label={A}
              [2,red,my label={C}]
              [2,red,my label={D}]
          ]
  ]]
      [2, my label={C}
    [1,red,my label={A}
              [2,red,my label={B}]
              [2,red,my label={D}]
          ]
    [1,red,my label={B}
              [2,red,my label={A}]
              [2,red,my label={D}]
          ]
          [1,red,my label={D}
              [2,red,my label={A}]
              [2,red,my label={B}, tier=this]
              ]
  ]
  [, tier=this, my label={D}
  [2
    [1,red,my label={A}
            [2,red,my label={B}]
            [2,red,my label={C}]
            ]
    [1,red,my label={B}
            [2,red,my label={A}]
            [2,red,my label={C}]
            ]
        [1,red,my label={C}
            [2,red,my label={A}]
            [2,red,my label={B}]
            ]
    ]]
]
\end{forest}
\end{document}
  • Exactly what I had hoped for as an alternative to my answer. +1. – hbaderts Sep 16 '16 at 2:44
  • @hbaderts Looks like the only votes for our answers are from us, then ;). – cfr Sep 16 '16 at 2:47
  • @cfr Wow! Also great. This Geometry package gives a lot of control, though a bit more work. – Dio Sep 16 '16 at 13:28
1

As your tree is very wide, and not too high, one possibility would be to place it on a landscape page. The pdflscape package with its landscape environment creates a new page in landscape mode, which doesn't mess up your header and is even displayed correctly in the PDF viewer:

\documentclass{article}

\usepackage{pdflscape}
\usepackage{forest}
\usepackage{lipsum}    % Only for creating example text

\begin{document}

\lipsum[1-4]    % Example text

\begin{landscape}
\begin{forest}
    ...
\end{forest}
\end{landscape}

\lipsum[5-7]    % Example text

\end{document}

Result with landscape mode

Of course, this solution will fail for very large forests. If that is the case, shrinking the whole forest (including text!) to fit the page might be an idea. An easy way is to use the adjustbox package, which allows you to scale any content to the desired size, e.g. by defining the width to be exactly the \linewidth:

\documentclass{article}

\usepackage{adjustbox}
\usepackage{forest}

\begin{document}

\begin{adjustbox}{width=\linewidth}
\begin{forest}
    ...
\end{forest}
\end{adjustbox}

\end{document}

Result with adjustbox

Of course, redesigning the forest to fit to your page without rescaling would be better, as you probably don't want to have many different, small font sizes in your document. Maybe a forest-expert can shed light on ways to redesign the forest in such a way.

  • Perfect! Thanks. Never heard of the latter package. – Dio Sep 15 '16 at 15:23

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.