# Forest trees fitting on page

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}

• 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, 2016 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, 2016 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)? Jun 29, 2018 at 12:02

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:

\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. Sep 16, 2016 at 2:44
– cfr
Sep 16, 2016 at 2:47
• @cfr Wow! Also great. This Geometry package gives a lot of control, though a bit more work.
– Dio
Sep 16, 2016 at 13:28

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}


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{forest}

\begin{document}