# qtree - labeling levels of a tree

In either tikz-qtree or qtree, is it possible to label or number different levels of a (vertical) tree? Such labels could be at the right or the left side of the whole tree.

• Don't know about tikz-qtree but I've done this with qtree. However, it is not at all straightforward. I would not do it this way now I've got better options. I would either use or modify an experimental solution I have based on forest. (Use if the tree is proof-like in structure, or modify otherwise.) But maybe this isn't an option for you. An MWE would obviously help. – cfr Sep 19 '15 at 22:15
• Warning: I've written a bunch of answers involving different versions of prooftrees.sty and justtrees.sty. A recent-ish example is tex.stackexchange.com/questions/256184/tree-and-blocks/…. But ask if you plan to use any of this and I'll give you a current version of the package. prooftrees is at version 0.08 and justtrees at version 0.04. If you search for prooftrees and look for answers by me, you can find some examples. – cfr Sep 19 '15 at 22:31
• Wow — that's beautiful. But I suspect it's more than I need. I'll keep things simple for now. – brannerchinese Sep 19 '15 at 23:47
• @brannerchinese For a simple tikz-qtree solution see How can I align captions with each level of a tree drawn with tikz-qtree? – Alan Munn Sep 19 '15 at 23:51

You can use \nodes to name some special nodes and allow getting access to their position in the tree and then use the named nodes to place the text. A little example placing text at both sides:

\documentclass[border=3pt]{standalone}
\usepackage{tikz-qtree}

\begin{document}

\begin{tikzpicture}
\Tree
[.\node (level0-right) {S};
[.NP
[.Det \node (level3-left) {the};
]
[.N cat
]
]
[.\node (level1-right) {VP};
[.V sat
]
[.\node (level2-right) {PP};
[.P on
]
[.\node (level3-right) {NP};
[.Det the
]
[.\node (level4-right) {N}; \node (level5-right) {mat};
]
]
]
]
]
\foreach \Value/\Text in {0/{Texto 1},1/{Texto 2},2/{Texto 3},3/{Texto 4},4/{Texto 5},5/{Texto 6}}
{
\node[anchor=west]
at ([xshift=1cm]{level5-right}|-{level\Value-right})
{\Text};
\node[anchor=east]
at ([xshift=-1cm]{level3-left}|-{level\Value-right})
{\Text};
}
\end{tikzpicture}

\end{document}


• (+1) But too much typing;. – cfr Sep 20 '15 at 0:40

Just for the heck of it, here's Gonzalo Medina's example tree with prooftrees:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{prooftrees}% version 0.8
\begin{document}
\begin{prooftree}
{
single branches
}
[S, just=explan 1
[NP, just=explan 2
[Det [the]]
[N [cat]]
]
[VP
[V [sat]]
[PP, just=explan 3
[P [on]]
[NP, just=explan 4
[Det, just=explan 5 [the, just=explan 6]]
[N [mat]]
]
]
]
]
\end{prooftree}
\end{document}


Note that the numbering on the left is automatic. (It can be switched off, but it is default.) Bear in mind, though, that the package is really designed specifically for logical proofs and not for other kinds of trees.

# EDIT

Here's the same tree with some overlay specifications in Beamer:

This is using the standard definitions I use for tikzpictures generally and forest trees, in particular:

\documentclass{beamer}
\usepackage{prooftrees}% version 0.8
\tikzset{% set up for transitions using tikz with beamer overlays
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
transparent/.style={opacity=0.1,text opacity=0.1},
opaque on/.style={alt=#1{}{transparent}},
}
\forestset{%
visible on/.style={%
for tree={%
/tikz/visible on={#1},
edge={/tikz/visible on={#1}}}},
opaque on/.style={%
for tree={%
/tikz/opaque on={#1},
edge={/tikz/opaque on={#1}}}},
for tree={%
}%
\begin{document}
\begin{frame}{A Tree}
\begin{prooftree}
{
single branches,
}
[S, just=explan 1
[NP, just=explan 2, alert on=<4->, for children={opaque on=<5->}
[Det [the]]
[N [cat]]
]
[VP
[V [sat]]
[PP, just=explan 3
[P [on]]
[NP, just=explan 4, alert on=<2>
[Det, just=explan 5, opaque on=<3-> [the, just=explan 6]]
[N [mat]]
]
]
]
]
\end{prooftree}
\end{frame}
\end{document}

• A lot of the interesting functionality of tikz-qtree doesn't work with beamer — "growing" trees upward, for instance. How does prooftree react to being in the same soup with beamer? – brannerchinese Sep 20 '15 at 18:34
• @brannerchinese Testing suggests it works similar to forest, which is quite well. You just need a few TikZ and forest styles and then you can use them within the tree. See edit for an example which uses transparency/opacity and alerted/regular transitions in the current example. – cfr Sep 20 '15 at 22:16
• @brannerchinese What makes you say that tikz-qtree doesn't work well with beamer? See the answer to tex.stackexchange.com/q/268578/3954 for an example of uncovering a tree upwards using tikz-qtree. – Gonzalo Medina Sep 20 '15 at 22:22
• Hm. Yes. It works now. – brannerchinese Sep 23 '15 at 9:27