# How do I align the last level of a tikZ \tree to the same height?

how do I get the very last hirachy of my tree to align to the same height? Every hierarchy before that is aligned but the last one differs.

Can you work with this code? I started working with TeX 5 weeks ago and it's the first time for me seeking help in a forum.

\documentclass[a5paper,pdftex,twoside,9pt,paper=21cm:14.85cm,openright]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}

\begin{center}
\resizebox{1\textwidth}{!}{%
\begin{tikzpicture}[every tree node/.style={align=center,anchor=north}]
\Tree [.\textbf{Gießverfahren}
[.{Schwerkraftgießen}
[.{Verlorene\\Formen}
[.{verlorene\\Modelle} \textit{Feingießen}\\\textit{Vollformgießem} ]
[.Dauerformen \textit{Kokillengießen}\\\textit{Stranggießen}\\\textit{Schleudergießen} ] ]
[.{\textbf{Gießen unter Druck}}
[.{Verlorene\\Formen} \textit{Niederdruck-Sandgießen} ]
[.\textbf{Dauerformen} \textbf{Druckgießen}\\\textit{Niederdruckgießen}\\\textit{Gegendruckgießen}\\\textit{Sonderverfahren} ] ] ]
\end{tikzpicture}}
\end{center}

\end{document}


frontier is a style which is applied to every leaf node. Assuming that 'the very last hierarchy' refers to these nodes i.e. the terminal nodes with no children, you can set this style to customise their appearance and placement.

To align these nodes, you need to specify an absolute distance from the root. (So this requires more fiddling than forest but allows you to work with your current code with few changes.)

For example:

\documentclass[tikz,border=10pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}
[
every tree node/.style={align=center,anchor=north},
frontier/.style={distance from root=130pt}
]
\Tree
[.\textbf{Gießverfahren}
[.{Schwerkraftgießen}
[.{Verlorene\\Formen}
[.{verlorene\\Modelle} \textit{Feingießen}\\\textit{Vollformgießem} ]
]
[.Dauerformen \textit{Kokillengießen}\\\textit{Stranggießen}\\\textit{Schleudergießen} ]
]
[.{\textbf{Gießen unter Druck}}
[.{Verlorene\\Formen} \textit{Niederdruck-Sandgießen} ]
[.\textbf{Dauerformen} \textbf{Druckgießen}\\\textit{Niederdruckgießen}\\\textit{Gegendruckgießen}\\\textit{Sonderverfahren} ]
]
]
\end{tikzpicture}
\end{document}


We can do something similar, but more flexibly, with forest by adapting Alenanno's solution:

  for tree={
parent anchor=south,
child anchor=north,


This ensures that the edges to the children start from the same point, as this is not default for forest, and are drawn to the top centre of those children.

    align=center,


does the same as in tikz-qtree.

It would be good to ensure that the nodes on all levels are aligned by default, if that's what is wanted, rather than relying on it happening to come out that way. We can do this using the following code:

    tier/.wrap pgfmath arg={tier #1}{level()},% align the nodes on all levels


We need to override this for the terminal nodes because we want them to be aligned despite being on different levels. So, for the leaf nodes, we set tier=word as in Alenanno's solution, but without having to remember to add it to each node:

    if n children=0{% for the leaf nodes
tier=word,% override the default tier setting because these are at different levels of the tree but we want them still aligned


While we're here, we might as well set these in italic font and override the exception, rather than setting them all in upright and then overriding that in all but one case:

      font=\itshape,% make these nodes italic by default and we'll override this for the single exception
}{}
}


For an introduction to forest and its bracket syntax, see the second part of my answer to an earlier question.

Forest code:

\documentclass[tikz,border=10pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
align=center,
tier/.wrap pgfmath arg={tier #1}{level()},% align the nodes on all levels
if n children=0{% for the leaf nodes
tier=word,% override the default tier setting because these are at different levels of the tree but we want them still aligned
font=\itshape,% make these nodes italic by default and we'll override this for the single exception
}{}
}
[\textbf{Gieverfahren}
[Schwerkraftgieen
[Verlorene Formen
[verlorene\\Modelle
[Feingieen\\Vollformgieen
]
]
[Dauermodelle
]
]
]
[Dauerformen
[Kokillengieen\\Stranggieen\\Schleudergieen
]
]
]
[\textbf{Gieen unter Druck}
[Verlorene Formen
[Niederdruck-Sandgieen
]
]
[\textbf{Dauerformen}
[\textbf{\upshape Druckgieen}\\Niederdruckgieen\\Gegendruckgieen\\Sonderverfahren
]
]
]
]
\end{forest}
\end{document}


Note that if you add \itshape inside the node, you need to do so after every line break, just as you would in tikz-qtree else only the first line will be set in italic.

I'd suggest using the forest package. It has the option tier=word that you can add to all the nodes you want to be aligned.

I have replaced some other minor details, feel free to ask for an explanation.

## Code

\documentclass[a5paper,pdftex,twoside,9pt,paper=21cm:14.85cm,openright]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usepackage{forest}

\forestset{
.style={
for tree={
base=bottom,
parent anchor=south,
child anchor=north,
align=center,
s sep=.9mm,
draw=none,
if n children=0{tier=word, font=\itshape}{}
}
}
}

\begin{document}

\begin{figure}[!h]
\centering
\resizebox{\textwidth}{!}{
\begin{forest}
[ \textbf{Gieverfahren}
[ Schwerkraftgieen
[ Verlorene Formen
[ verlorene\\Modelle
[Feingieen\\Vollformgieen] ]
[ Dauermodelle
[ Dauerformen
[Kokillengieen\\Stranggieen\\Schleudergieen] ] ]
[ \textbf{Gieen unter Druck}
[ Verlorene Formen
[Niederdruck-Sandgieen] ]
[ \textbf{Dauerformen}
[\textbf{Druckgieen}\\Niederdruckgieen\\Gegendruckgieen\\Sonderverfahren] ] ] ]
\end{forest}}
\end{figure}
\end{document}

• @cfr You were right, I didn't notice that. It does sound inefficiently redundant. I'll add the command to the forestset. :) – Alenanno Dec 1 '15 at 1:21

It is also possible to insert the missing levels:

\documentclass[a5paper,twoside,fontsize=9pt,open=any]{scrartcl}
\usepackage[ngerman]{babel}
\usepackage{selinput}

\usepackage{tikz-qtree}

\begin{document}
\begin{center}
\resizebox{1\textwidth}{!}{%
\begin{tikzpicture}[every tree node/.style={align=center,anchor=north}]
\Tree [.\textbf{Gießverfahren}
[.{Schwerkraftgießen}
[.{Verlorene\\Formen}
[.{verlorene\\Modelle} \textit{Feingießen}\\\textit{Vollformgießem} ]
[.Dauerformen
[
[.\textit{Kokillengießen}\\\textit{Stranggießen}\\\textit{Schleudergießen} ] ] ] ]
[.{\textbf{Gießen unter Druck}}
[.{Verlorene\\Formen}
[
[.\textit{Niederdruck-Sandgießen} ] ] ]
[.\textbf{Dauerformen}
[
[.\textbf{Druckgießen}\\\textit{Niederdruckgießen}\\\textit{Gegendruckgießen}\\\textit{Sonderverfahren} ] ] ] ] ]
\end{tikzpicture}}
\end{center}
\end{document}