# Adding vertical hierarchy lines to a category column of a table

I often need to represent hierarchies within tables but I've yet to find an elegant way to do this. Oftentimes I do a variation on the following theme, with nested whitespace in the category column:

\documentclass{article}
\usepackage{booktabs}

\newcommand{\lvl}{~~~}

\begin{document}

\begin{tabular}{lr}
\toprule
\textbf{Category} & \textbf{\%}\\
\midrule
Animal          & 100   \\
\lvl Human      & 50    \\
\lvl\lvl  Man   & 20    \\
\lvl\lvl  Woman & 30    \\
\lvl Fox        & 30    \\
\lvl\lvl Vixen  & 16    \\
\lvl\lvl Dog    & 14    \\
\lvl Chicken    & 20    \\
\lvl\lvl Cock   & 8     \\
\lvl\lvl Hen    & 12    \\
\bottomrule
\end{tabular}

\end{document}


I might mix that up with midrules between categories and different fonts for different levels and so on. But the result can sometimes be very "busy", especially if there's a bunch of levels.

Instead, I was playing around with the idea of adding lines to more elegantly represent the hierarchy, sketched as follows in red (a similar effect in the same spirit would also suffice):

But I'm not sure how to realise this in LaTeX in an elegant way. I'm guessing I could somehow decorate a table with TikZ but I'd only know how to specify manual points (e.g., I'm not sure how to anchor rows in a table).

Thus I'm looking for any suggestions of elegant solutions to achieve the above effect. (An ideal answer would avoid explicit coordinates/distances and would be "highly reusable".)

-
Have a look at the dirtree package. –  Peter Grill Aug 8 '14 at 20:56
@Peter Grill, thanks! I had a look and added an answer explaining what I found in that package. It's not ideal but it's definitely a workable solution! –  badroit Aug 8 '14 at 21:35

You might be interested in a solution with pst-node, more precisely its psmatrix environment, and some \rlaps. This solution supports colour:

\documentclass[12pt, a4paper, pdf, x11names]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{MinionPro}

\usepackage{pst-node}

\begin{document}
\begin{pspicture}
\psset{rowsep=0.5ex,  colsep=0.4, angleA=-90, angleB=180,mcol=l, nodesep =1ex, linewidth=0.4pt, linecolor=red, }
\def\pscolhookiv{\psset[pst-node]{mcol =r, colsep = 1cm, href = 1}}
\begin{psmatrix}
[name = Cat]\pnode[-0.5em]{c}\rlap{\textbf{Category}} &  &  & [name = Pct]\textbf{\%}\pnode[0.5em]{t}\\[0.6ex]
\midrule
[name = A] A\rlap{nimal} &  &  & 100 \\
& [name = H] \makebox[0.8em][l]{Human} &  & 50 \\
&  & [name = HM]Man  & 20 \\
&  & [name = HF] Woman  & 30 \\
& [name = F]  \makebox[0.8em][l]{Fox}  &  & 30 \\
&  & [name = FF] Vixen  & 16 \\
&  & [name = FM] Dog  & 14 \\
& [name = C] \makebox[0.8em][l]{Chicken}  &  & 20 \\
&  & [name = CM] Cock  & 8 \\
\pnode[-0.5em]{d} &  &[name = CF] Hen  & 12\pnode[0.5em]{u} \\
\end{psmatrix}
\ncangle{A}{H}\ncangle{A}{F}\ncangle{A}{C}
\ncangle{H}{HM}\ncangle{H}{HF}
\ncangle{F}{FM}\ncangle{F}{FF}
\ncangle{C}{CM}\ncangle{C}{CF}
\psset{linecolor = black}
\ncline[offset = -1.4ex]{c}{t}
\psset{linewidth = 0.8pt}
\ncline[offset =2.5ex]{c}{t}
\ncline[offset =-1.25ex]{d}{u}
\end{pspicture}
\end{document}


-
Thanks! This looks great! Versus dirtree, there's a bit more configurability with this solution with regards the formatting of the lines and and the solution maintains the structure of the table. I guess the main drawback is the incompatibility with pdftex and there's a bit more coding required to create the levels and hooks for the hierarchy. –  badroit Aug 10 '14 at 20:04
@badroit: It is compatible with pdflatex. Didn't you notice the pdf option to \documentclass? It tells pstricks to launch autopstpdf: the latter extracts the pstricks part of the doc as a separate file, compiles it and makes a pdf image out of it, and reincorporates it to the main document's .pdf. All you have to do is to launch pdflatex with enable-write18 switch (MiKTeX) or --shell-escape (TeX Live, MacTeX). As for the code, it is very structured: you define the elements of a psmatrix, then the links between them. –  Bernard Aug 10 '14 at 20:37
thanks! I wasn't aware of the autopstpdf option! :) –  badroit Aug 10 '14 at 20:53

Thanks to Peter Grill's suggestion to try the dirtree package, I managed to find this question here, which led me to try this solution:

\documentclass{article}
\usepackage{dirtree,array}
\usepackage{booktabs}

\begin{document}

\renewcommand{\DTstyle}{\textrm}
% if you need to fiddle with row space
% in the tree
%\setlength{\DTbaselineskip}{20pt}

\begin{tabular}{lrr}
\toprule
\textbf{Category} & \textbf{\% (tree)} & \textbf{\%}\\
\midrule

\DTsetlength{0.2em}{0.7em}{0.2em}{0.4pt}{0pt}
\begin{minipage}{2cm}\dirtree{%
.1 Animal.
.2 Human.
.3 Man.
.3 Woman.
.2 Fox.
.3 Vixen.
.3 Dog.
.2 Chicken.
.3 Cock.
.3 Hen.
}\end{minipage}
&
\DTsetlength{0pt}{0pt}{0pt}{0pt}{0pt}
\begin{minipage}{0.4cm}\dirtree{%
.1 100.
.1 50.
.1 20.
.1 30.
.1 30.
.1 16.
.1 14.
.1 20.
.1 8.
.1 12.
}\end{minipage}
&
\begin{tabular}{r}%
100\\
50\\
20\\
30\\
30\\
16\\
14\\
20\\
8\\
12
\end{tabular}
\\
\bottomrule
\end{tabular}
\end{document}


It's a little rough around the edges, but it works fairly well. I still need to figure out the horizontal alignment of the columns with the headers (e.g., how best to remove the whitespace to the left of the hierarchy or what causes it).

I show two options for doing the second column: one using dirtree which ensures the same spacing, and one using tabular which allows for changing the column alignment (but whose vertical alignment may need fiddling with).

In terms of the code, it's pretty easy to configure the hierarchies and to modify the depth of the levels and so forth. Any manual dimensions are table-level.

The biggest disadvantage is that you have to format an entire column as one cell. This might be problematic in many cases, such as those involving multi-row or if you wanted to add midrules, etc.

As such, though this solution is definitely workable, still open to alternatives that integrate better with tables. :)

-
I believe there are setting in the dirtree package to control the various spaces. –  Peter Grill Aug 8 '14 at 21:45
Yup, the example mentions all the options in the package to set the space but there's no options for the initial indent that I can see. However, the \DTsetlength{0.2em}{0.7em}{0.2em}{0.4pt}{0pt} command (explained in the doc) allows for setting the indentation, line width, dot size, etc. and \setlength{\DTbaselineskip}{20pt} allows for changing the row spacing. –  badroit Aug 8 '14 at 21:49