This is what I have:
1) How to make sure that the words don't break and are on the same line?
2) How to highlight the lower right box "Sub-sub-sub-total2 80,000" by making the box bolder or circular, and text bolder too?
My code is:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees}% cfr added this as the code fails to compile otherwise
\begin{document}
\begin{center}
\begin{tikzpicture}[level distance=1.75in,sibling distance=.25in,scale=0.75]
\tikzset{edge from parent/.style=
{thick, draw,
edge from parent fork right},every tree node/.style={draw,minimum width=1.5in,text width=1in, align=center},grow'=right}
\Tree
[. {Total \\ 500,000}
[.{Sub-total1 \\ 250,000}
]
[. {Sub-total2 \\ 250,000}
[.{Sub-sub-total1 \\ 50,000} ]
[.{Sub-sub-total2 \\ 200,000}
[.{Sub-sub-sub-total1 \\ 120,000} ]
[.{Sub-sub-sub-total2 \\ 80,000} ]
]
]
]
\end{tikzpicture}
\end{center}
\end{document}
P.S. And actually, I find this diagram I created quite ugly. I think there is a lot of unused space between the boxes. Can you help me make it compact and easy to read? Thank you.
Best Answer
If you are open to using
forest
, it can easily do what you want. Forest is a TikZ-based package dedicated to drawing trees. In particular, it is designed to produce (optionally but by default) extremely compact trees. It runs through multiple cycles, first typesetting the nodes, then packing them, then calculating their locations and only drawing the tree when all the calculations are complete. It is also extremely flexible and powerful.In this case, you can simply allow Forest to figure out the width needed for the nodes and the spacing between them required for a compact but attractive-looking tree.
The
edges
library provides theforked edges
option similar to thetrees
library'sedge from parent forked ...
. TikZ options can freely be used to style the tree.To use the
edges
library, we can just pass it as a package option.Then we can use
forked edges
to style the tree.We also want to change the default direction of growth from the default southwards to eastwards. We can use
grow'=0
for this (the'
just stops the change altering the order in which the children are drawn). We want our changes to apply to the whole tree, so we usefor tree={...}
.We want each node to use
draw
and we want the text to be centred.I suggest using sans for the text of the nodes. While it is a mistake to overuse sans, it often works well in diagrams, especially quite compact ones.
I think that rounding the corners of the nodes borders also makes the tree a little prettier, without being in any way fussy.
Obviously, this is all optional and can easily be omitted if you prefer.
The
highlight
style is defined for easy highlighting of nodes.If you only need this for one node, you may prefer to add the options directly. I tend to create styles at the drop of the proverbial hat on the grounds that I might apply them more generally and to keep things tidy.
Then we just let Forest do its thing ...
Complete code:
Code for Forest v.1
The above code will work with current Forest. If you only have version 1 and cannot update, you will get a compilation error and will need the following code instead.