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:




\textbf{Category} & \textbf{\%}\\
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    \\


enter image description here

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):

enter image description here

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

  • 1
    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}



\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}}
    [name = Cat]\pnode[-0.5em]{c}\rlap{\textbf{Category}} &  &  & [name = Pct]\textbf{\%}\pnode[0.5em]{t}\\[0.6ex]
    [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} \\
    \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}

enter image description here

  • 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
  • 1
    @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:



% if you need to fiddle with row space
% in the tree

\textbf{Category} & \textbf{\% (tree)} & \textbf{\%}\\

.1 Animal.
.2 Human.
.3 Man.
.3 Woman.
.2 Fox.
.3 Vixen.
.3 Dog.
.2 Chicken.
.3 Cock.
.3 Hen.
.1 100.
.1 50.
.1 20.
.1 30.
.1 30.
.1 16.
.1 14.
.1 20.
.1 8.
.1 12.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.