[Tex/LaTex] Horizontal tree in TikZ picture, LaTeX

tikz-pgftikz-trees

This is what I have:

enter image description here

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 the forked edges option similar to the trees library's edge 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.

\usepackage[edges]{forest}

Then we can use forked edges to style the tree.

\begin{forest}
  forked edges,

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 use for tree={...}.

  for tree={
    grow'=0,

We want each node to use draw and we want the text to be centred.

    draw,
    align=c,

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.

    font=\sffamily,

I think that rounding the corners of the nodes borders also makes the tree a little prettier, without being in any way fussy.

    rounded corners,
  },

Obviously, this is all optional and can easily be omitted if you prefer.

The highlight style is defined for easy highlighting of nodes.

  highlight/.style={
    thick,
    font=\sffamily\bfseries
  }
  [{Total\\500,000}

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 ...

Forest tree

Complete code:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
  forked edges,
  for tree={
    grow'=0,
    draw,
    align=c,
    font=\sffamily,
    rounded corners,
  },
  highlight/.style={
    thick,
    font=\sffamily\bfseries
  }
  [{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}, highlight]
      ]
    ]
  ]
\end{forest}
\end{document}

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.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}% for version 1
\begin{document}
\begin{forest}
  for tree={
    grow'=0,
    draw,
    align=c,
    font=\sffamily,
    rounded corners,
    parent anchor=east,
    child anchor=west,
    edge path={%
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(5pt,0) |- (.child anchor)\forestoption{edge label};
    }
  },
  highlight/.style={
    thick,
    font=\sffamily\bfseries
  }
  [{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}, highlight]
      ]
    ]
  ]
\end{forest}
\end{document}

version 1 version