UPDATE Code works without modification with the new version (2) of forest.
- The tree is asymmetric. So, the default alignment is by level. How do I align all leaves at the same level?
This can be accomplished by setting tier=<tier name>
for all terminal nodes. To do this automatically, add this code to for tree
:
if n children=0{
tier=terminal
}{}
- How do I make some nodes squares while others remain circles?
You can add shape=rectangle
to those you want to be non-circles. If you want to force them to be squares, you can text their width and height but this might have odd results. Or is there a pattern to the ones you want to be square and the ones you want to be circular?
- How do I color a path a different color from root to a particular leaf?
You can use node walk
to trace the path back from leaf to root. For example, here's a style which takes one argument (the colour) and colours the path back to the root in that colour:
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
}
}
To use it, you just add colour my roots=<colour>
to the node of your choice. (Doesn't have to be a leaf - it will start from wherever it is. Note that if you use this style more than once, the root node will end up in the last colour used, as will any common path segments.
Here's a simple example:
\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{forest}
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
},
my edge label/.style={
edge label={
node [midway, fill=white, font=\scriptsize] {#1}
}
}
}
\begin{document}
\begin{forest}
for tree={
circle,
inner sep=1pt,
l sep+=5pt,
draw,
align=center,
if n children=0{
tier=terminal
}{},
}
[HP
[IP, my edge label=NB
[ABC
[DEF]
[GEH, colour my roots=blue]
]
[HIJ
[LMN, colour my roots=red
[OPQ]
]
[RST]
]
]
]
\end{forest}
\end{document}
Here's a sample result:
- I use \resizebox to fit the tree in the page. Assuming people who're seeing the tree turn the page into landscape mode, is there a global option I can set to turn all node text vertically?
Don't. This complicates things unnecessarily. Instead, draw the tree with the default direction of growth and put it in a landscape
environment. (Or use sidewaysfigure
if you don't want the page itself to be turned.
Complete code:
\documentclass[a5paper]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{forest,pdflscape}
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
}
}
\begin{document}
\newlength\myheight
\setlength\myheight{\textheight}%
\begin{landscape}
\noindent
\resizebox{\myheight}{!}{%
\begin{forest} %Figure out how to align edge labels with the edge
for tree={
circle,
draw,
align=center,
if n children=0{
tier=terminal
}{}
}
[\tiny{HP}
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}} %not built
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}} % not built and closed
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}, for tree={shape=rectangle}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}, colour my roots=blue]
]
] %Last line copied for NB and closed. No Ip.
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}, shape=rectangle
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}, colour my roots=red]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}} % not built and open
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP}
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}} % built
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}} % built and closed
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}} % built and Open
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}, colour my roots=green!50!black]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
]
]
]
\end{forest}%
}%
\end{landscape}
\end{document}
This makes minimal changes to your code. Do note, however, that you could make life much, much simpler for yourself if you used a couple of styles rather than repeating code so often.
In fact, just adding font=\tiny
to for tree
would save writing \tiny{}
around the content of every node.
If you can manage a style as well, the labels can be simplified greatly. If there is a pattern to the labels or content, you could automate the tree further. If not, you'd get something like this:
\documentclass[a5paper]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{forest,pdflscape}
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
},
my edge label/.style={
edge label={
node [midway, fill=white, font=\scriptsize] {#1}
}
}
}
\begin{document}
\newlength\myheight
\setlength\myheight{\textheight}%
\begin{landscape}
\noindent
\resizebox{\myheight}{!}{%
\begin{forest} %Figure out how to align edge labels with the edge
for tree={
circle,
draw,
align=center,
if n children=0{
tier=terminal
}{},
font=\tiny
}
[HP
[IP, my edge label=NB %not built
...
]
]
\end{forest}%
}%
\end{landscape}
\end{document}
which is still much less cluttered and, certainly, less repetitive typing!
Finally, if your actual tree is anything at all like this, you need to rethink the way you are presenting this information.
Nobody will be able to read anything this minute. Even if people read electronically only and can zoom away (but why stick to a tiny page area in that case? Really big electronic paper is still cheap!), it is not going to be at all obvious that there is anything much worth zooming for.
At best, you will irritate your readers. At worst, they will simply give up in frustration.
Best Answer
I would:
geometry
to get more sensible margins;my label
in the example below;pdflscape
for larger trees (not needed for this one).Here's an example: