# How to set optimal level distance in qtree when different nodes have very different amount of text?

I am really a newbie in tikz (about 12 hrs experience) but I think I need to some hint on this.

I am using tikz-qtree. Some of the nodes can contain more text than others. I set the text width to a value so that a node does not expand too much side-ways.

Then by trial and error, I found a value for level distance such that the edges do not overlap with the content of the nodes.

The problem is that the level distance affects all the levels equally. So this results in some levels having too much empty space between them.

Better to explain by an example. Here is a screen shot of what I have now when I set the level distance=5cm

You can see in the above, that there is lots of empty space now between top level and the next level, and between the third level and the 4th level. While the distance between the first level and the next one is OK.

If I reduce the value of level distance to 3cm, then I get this problem:

What is the correct way to do this, so that minimum space is wasted between all levels?

I looked around and could not find a way to set the level distance per one level using qtree package. The best I found is this How to change the level distance in tikz-qtree for one level only? and after reading it few times, can't understand it or what to do.

May be someone here could modify the code below to show me how to do it?

Here is the code I used to generate the above. I did not want to remove things from it to make smaller since it already show the problem.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees}
\begin{document}
\tikzset{font=\small,edge from parent fork down,level distance=3cm,
every node/.style={draw,rectangle,rounded corners,
align=center,text width = 100pt }}
\begin{tikzpicture}
\Tree [.{second order linear partial differential equation}
[.{elliptic\\No characteristic curves
\\diffusion process  reached equilibrium, steady state temperature distribution.
Numerically, solved by relaxation methods
\\{$A\frac{\partial^{2}u}{\partial x^{2}}+B\frac{\partial^{2}u}{\partial x\partial y}+C\frac{\partial^{2}u}{\partial y^{2}}+D\frac{\partial u}{\partial x}+E\frac{\partial u}{\partial y}+Fu=G$}
\\{$B^2-4AC<0$}
\\{$A=1,C=1,B=0$}}
[.{{$F=0$}\\a function that satisfies Laplace is called harmonic}
[.{{$G=0$}\\Laplace in 2D\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}}=0$}     } ]
[.{{$G=g(x,y)$}\\Poisson in 2D\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}}=g(x,y)$}} ]
]
[.{$F=k^2$}
[.{{$G=0$}\\homogeneous Helmholtz\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}} + k^2 u=0$}\\eigenvalue equilibrium} ]
[.{{$G=g(x,y)$}\\inhomogeneous Helmholtz\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}} + k^2 u=g(x,y)$}\\steady state oscillation} ]
]
]
[.parabolic ]
[.hyperbolic ]
]

\end{tikzpicture}
\end{document}


The fork down path from the regular TikZ trees library interacts in weird ways with tikz-qtree sometimes. I've replaced the fork down path with the method suggested in the tikz-qtree manual and then set individual level distances for each level in the tree. This gives you a pretty reasonable tree.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
%\usetikzlibrary{trees}        % not required for this solution
\begin{document}
\tikzset{font=\small,
edge from parent path={(\tikzparentnode.south) -- +(0,-8pt) -| (\tikzchildnode)},
level 1/.style={level distance=2cm},
level 2/.style={level distance=6cm},
level 3/.style={level distance=2cm},
every node/.style={draw,rectangle,rounded corners, align=center,text width = 100pt}
}
\begin{tikzpicture}
\Tree [.{second order linear partial differential equation}
[.{elliptic\\No characteristic curves
\\diffusion process  reached equilibrium, steady state temperature distribution.
Numerically, solved by relaxation methods
\\{$A\frac{\partial^{2}u}{\partial x^{2}}+B\frac{\partial^{2}u}{\partial x\partial y}+C\frac{\partial^{2}u}{\partial y^{2}}+D\frac{\partial u}{\partial x}+E\frac{\partial u}{\partial y}+Fu=G$}
\\{$B^2-4AC<0$}
\\{$A=1,C=1,B=0$}}
[.{{$F=0$}\\a function that satisfies Laplace is called harmonic}
[.{{$G=0$}\\Laplace in 2D\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}}=0$}     } ]
[.{{$G=g(x,y)$}\\Poisson in 2D\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}}=g(x,y)$}} ]
]
[.{$F=k^2$}
[.{{$G=0$}\\homogeneous Helmholtz\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}} + k^2 u=0$}\\eigenvalue equilibrium} ]
[.{{$G=g(x,y)$}\\inhomogeneous Helmholtz\\{$\frac{\partial^{2}u}{\partial x^{2}}+ \frac{\partial^{2}u}{\partial y^{2}} + k^2 u=g(x,y)$}\\steady state oscillation} ]
]
]
[.parabolic ]
[.hyperbolic ]
]

\end{tikzpicture}
\end{document}


Alternatively, you could make the node that has the huge amount of text in it wider by creating a wide node style and applying it to that node. This would allow you to keep the level distances smaller.

For the next image, I added

wide node/.style={text width=200pt}


to the tikzset command, and changed the level 2 distance:

level 2/.style={level distance=4cm}


Then for the large node I added an explicit \node command:

[.\node[wide node]{elliptic\\No characteristic curves ... };


Don't forget that this needs an explicit ; after the node text since it is a regular tikz command.

This produces: