# Making a (simple) directory tree

I'm trying to make a simple directory tree representation in LaTeX. Something like:

spam
|
+-- ham
+-- eggs
|
+-- more spam
+-- dead parrots


The tree won't be that long (say, 10--15 entries or so), with three or so levels, and is a one-off thing. So, quick-and-dirty solutions are welcome. When recommending tikz1, please note that I have almost no experience with it, so I'd appreciate a clean simple solution, even if it doesn't look quite perfect.

1 On Tex.SE tikz is almost like the jquery recurring joke on StackOverflow..

Edit: As per Thorsten's suggestion, dirtree is a nice and easy-to-use solution. The example from above could be written as:

\usepackage{dirtree}
...
\dirtree{%
.1 spam.
.2 ham.
.2 eggs.
.3 more spam.
.3 dead parrots.
}

-

## This question has an open bounty worth +50 reputation from yo' ending in 5 days.

The current answer(s) are out-of-date and require revision given recent changes.

I would really like to see this done in a way that allows customization, possibly using TikZ. The dirtree interface seems good, but the programming layer seems not so good. Maybe it exists (that why only 50 bounty at the moment, there may be a good package for it or this question may even be a dupe), but I'm willing to donate much more for a nice solution!

–  cfr yesterday

## 2 Answers

The dirtree package is your friend.

-
Unfortunately dirtree is in conflict with algorithmic and pseudocode. So it's useless in many cases. –  user6052 Jun 9 '11 at 12:11

Here's one option using the powerful forest package; since its built upon TikZ the customization possibilities are enormous.

For example one can easily simulate the style produced by dirtree:

\documentclass[border=5pt]{standalone}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[text1
[text1.1
[text1.1.1]
[text1.1.2]
[text1.1.3]
]
[text1.2
[text1.2.1]
[text1.2.2]
]
]
\end{forest}

\end{document}


And a simple modification to the previous specifications

  for tree={
font=\sffamily,
text=white,
text width=2cm,
minimum height=0.75cm,
if level=0
{fill=mygreenii}
{fill=mygreeni},
rounded corners=4pt,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge={mygreenii,rounded corners,line width=1pt},
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
s sep=15pt,
before computing xy={l=15pt},
}


now gives:

The initial theme can also be easily changed to

\documentclass[border=5pt]{standalone}
\usepackage{forest}

\definecolor{folderbg}{RGB}{124,166,198}
\definecolor{folderborder}{RGB}{110,144,169}

\def\Size{4pt}
\tikzset{
folder/.pic={
\filldraw[draw=folderborder,top color=folderbg!50,bottom color=folderbg]
(-1.05*\Size,0.2\Size+5pt) rectangle ++(.75*\Size,-0.2\Size-5pt);
\filldraw[draw=folderborder,top color=folderbg!50,bottom color=folderbg]
(-1.15*\Size,-\Size) rectangle (1.15*\Size,\Size);
}
}

\begin{document}

\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
inner xsep=7pt,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- (.child anchor) pic {folder} \forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}
[system
[config
]
[lib
[Access
]
[Plugin
]
]
[templates
]
[tests
]
]
\end{forest}

\end{document}


which produced

And combining these settings with a variation of my answer to Draw directory tree using folder icons one can produce

\documentclass[border=10pt]{standalone}
\usepackage{forest}

\definecolor{fblue}{RGB}{92,144,192}
\definecolor{fgreen}{RGB}{34,162,70}

\newcommand\myfolder[2][fblue]{%
\begin{tikzpicture}[overlay]
\begin{scope}[xshift=20pt]
\filldraw[rounded corners=1pt,fill=#1,draw=white,double=black]
(-23pt,10pt) -- ++(3pt,5pt) -- ++(18pt,0pt) -- ++(40:3pt) -- ++(9pt,0pt) -- ++(-40:3pt)
-- (20pt,15pt) -- (23pt,10pt) -- cycle;
\filldraw[rounded corners,draw=white,double=black,top color=#1,bottom color=#1!30]
(-22pt,-12pt) -- ++(44pt,0pt) -- (25pt,12pt) coordinate (topr) -- ++(-50pt,0pt) coordinate (topl) -- cycle;
\end{scope}
\end{tikzpicture}%
\makebox[35pt]{\raisebox{-3pt}{{\ttfamily/#2}}}%
}

\begin{document}

\begin{forest}
for tree={
font=\sffamily,
minimum height=0.75cm,
rounded corners=4pt,
grow'=0,
inner ysep=8pt,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge={rounded corners},
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(12.5pt,0) |- (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom,minimum height=18pt]}}
{}
},
fit=band,
s sep=12pt,
before computing xy={l=25pt},
}
[\myfolder{}
[\myfolder{bin}]
[\myfolder{dev}]
[{\myfolder[fgreen]{home}}
[{\myfolder[fgreen]{anna}}]
[{\myfolder[fgreen]{tess}}]
]
[\myfolder{media}]
[\myfolder{mnt}]
[\myfolder{usr}]
]
\end{forest}

\end{document}


What should be noticed here is that once the basic specification for the vertical structure has been established (which I borrowed with some modifications from cfr (See, for example her answer to how to adjust large number of nodes in forest environment?)):

  for tree={
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=15pt},
}


all one has to do is to let the imagination fly and produce as many variations as desired using the power of forest/TikZ.

-
Awesome answer. Just out of curiosity, is this a new package? Cause the question is ~5 years old. –  Rakesh yesterday
@Rakesh I have started a bounty on the question to draw some attention to it. My motivation for the bounty is that a recent question was closed as a duplicate of this one, but I didn't find the answers here satisfactory. –  yo' yesterday
@yo' What other kind of variation/customization would you like to see? –  Gonzalo Medina yesterday
@GonzaloMedina I like it as a proof of concept. Once it's clear you can easily do it with forest, everybody can use any customization this package offers to provide the result. Awesome answer! –  yo' yesterday
@GonzaloMedina Does it have any 'folder' structure (like the last one you added) that is little bit subtler. Like something that we see in 'Windows Explorer' and in 'Mac's Finder'? I mean to ask, the text follows the icon, and not inside that –  Rakesh yesterday