8

I would like to put a tree list into a table environment. Smth. like:

+--------------------------+---------------------------+-------------+
| Heading 1                |  Heading 2                | Heading 3   |
+--------------------------+---------------------------+-------------+
| / main-item              |  main item data           | some info   |
|   +-- sub item 1         |  sub item 1 data          | some info 1 |
|   |   +-- sub sub item x |  sub sub item 1.x data    | some info x |
|   |                      |                           |             |
|   +-- sub item 2         |  sub item 2 data          | some info 2 |
|       +-- sub sub item y |  sub sub item 2.y data    | some info y |
+--------------------------+---------------------------+-------------+

I know about dirtree package, but it it possible to arrange dirtree data in that way?

P.S.: I received multiple answers to this question, which solve the problem. I personally found the accepted answer more appropriate in my case. But here are also answers how to achieve the requested functionality with dirtree package. Therefore I encourage everyone to read all answers to find the best possible solution.

2

Here an example how you can mimic that with pure table environment. But you don't have connections between sub-items.

Hope that helps anyway.

\begin{table}[p]
\begin{center}
    \caption{Some caption of your table} \label{tab:trendtable}
    \begin{tabular}{lll}
    \midrule[1pt]
    \multicolumn{1}{c} {Heading 1} & \multicolumn{1}{c}{Heading 2} & \multicolumn{1}{c} {Heading 3} \\ 
    \midrule[1pt]
    \textbf{main-item}             & main item data        & some info\\
    \hspace{0.2cm} sub item 1      & sub item 1 data       & some info 1 \\
    \hspace{0.4cm} sub sub item x  & sub item 1.x          & some info x \\
    &&\\
    \textbf{main-item 2}           & ...                   & ...\\
    \hspace{0.2cm} ...             & ...                   & ... \\
    \end{tabular}
\end{center}
\end{table} 

compiled code as an image

  • While this seems to have been accepted by the questioner, it doesn't use dirtree, and since the answer posted by Yiannis (along with my comment) actually solves the problem with dirtree it should be preferred as an answer. – Alan Munn Jan 11 '11 at 16:30
  • @Alan: Please see my comment in the post of Yiannis, regarding your answer. IMO this answer deserves a + point! – dusha Jan 11 '11 at 16:43
  • Instead multicolumn{1}{c} {Heading 2} it should be \multicolumn{1}{c} {Heading 2}! – cmur2 May 31 '13 at 17:13
6

You can use dirtree within a table, just enclose the mark-up within a minipage and be careful with the stops at the end of the lines! Here is a minimal example:

\documentclass{article} 
\usepackage{dirtree}
\begin{document}
\begin{tabular}{|l|l|}
\begin{minipage}{4cm}\dirtree{%
.1 Engineering.
.2 Mechanical.
.3 Electrical.
.4 Electronics.
}\end{minipage}    
& \begin{minipage}{4cm}\dirtree{%
.1 TeX.
.2 LaTeX.
.3 XeTeX.
.4 pdfLaTeX.
}\end{minipage}\\
\end{tabular}
\end{document}

Image of first code

Edit as per comments

\begin{tabular}{|l|p{3cm}|}
\begin{minipage}[t]{4cm}\dirtree{%
.1 Engineering.
.2 Mechanical.
.3 Electrical.
.4 Electronics.
}\end{minipage}    
&  Director A  
   \newline Director B 
   \newline Director C
   \newline Director D\\
\end{tabular}

This will produce an output as requested (two columns) the third is trivial! For anything more fancy I would use a graphics package such as TikZ or pstricks. This sort of layout IMHo is better produced using, just minipages and or specially constructed environments. Nevertheless dirtree is a great package and one can use it for all sorts of different problems.

image of second code

  • Thanks for the answer. But unfortunately that example generates 2 dirtrees side-by-side. I would like to have just usual data associated with each dirtree item, but this data should be aligned in the table. It is possible that I might have 3 or more columns. You can think of it as some kind of a TreeListView control: codeproject.com/KB/list/treelistview.aspx – dusha Jan 11 '11 at 15:10
  • 1
    @Dusha: Make an extra \dirtree for each of your subsequent columns, and use \DTsetlength with everything set to 0. (or if you still want indenting for the levels, set the width parameter.) – Alan Munn Jan 11 '11 at 15:46
  • Alan thanks a lot. I did not explicitly require to use dirtree. I just stated that I know about that package. Anyway I find the accepted answer easier to use and maintain. It is more visual to have related items on one line, instead of counting the lines of subsequent dirtrees just to find out the relation. If you gave a minus point to the accepted answer, I find it a petty, since I really like the simplicity of the idea. – dusha Jan 11 '11 at 16:41
  • @dusha: Well, I can't argue with which you think answers your question, but your accepted solution also requires a lot of hand spacing, which is probably hard to maintain too. If others who do want to use dirtree in this way arrive at your question, they may think there is no solution using it if the accepted answer doesn't use it. – Alan Munn Jan 11 '11 at 16:51
  • @Yiannis: I've put a full example as an answer for completeness. – Alan Munn Jan 11 '11 at 16:52
4

Here is a direct solution (based entirely off of Yiannis's answer) which solves the problem.

\documentclass{article} 
\usepackage{dirtree,array}
\setlength{\extrarowheight}{3pt}
\begin{document}
\begin{tabular}{|l|l|l|}
\hline
\multicolumn{1}{|c} {Heading 1} & \multicolumn{1}{|c|} {Heading 2} & \multicolumn{1}{c|}{Heading 3}\\
\hline
\begin{minipage}{4cm}\dirtree{%
.1 Engineering.
.2 Mechanical.
.3 Bio-mechanical.
.2 Electronics.
}\end{minipage}
&
\DTsetlength{0pt}{0pt}{0pt}{0pt}{0pt}
\begin{minipage}{6cm}\dirtree{%
.1 Stuff about engineering here.
.2 Stuff about Mechanical here.
.3 Stuff about biomechanical here.
.4 .
}\end{minipage}
&
\DTsetlength{0pt}{0pt}{0pt}{0pt}{0pt}
\begin{minipage}{4cm}\dirtree{%
.1 Comments.
.2 More comments.
.3 Still more.
.4 And more.
}\end{minipage}\\
\hline
\end{tabular}
\end{document}
&
\DTsetlength{0pt}{0pt}{0pt}{0pt}{0pt}
\begin{minipage}{6cm}\dirtree{%
.1 Stuff about engineering here.
.2 Stuff about Mechanical here.
.3 Stuff about Electrical here.
.4 .
}\end{minipage}
\end{tabular}
\end{document}

Image of compiled code

  • Thanks for posting your answer using \DTsetlength, it is a very clean solution and programmabel, i.e one can define \addline{} and do a row etc that can work on both sides. – Yiannis Lazarides Jan 11 '11 at 16: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.