# filesystem tree diagram shows strange iteration behaviour

I found this (the one on the left) on TeXample.net.
I made the one on the right, based on the same code. I was hoping I might be a be able to write a script to map out a real filesystem. I decided to start small. I tried to recreate it with a single \foreach loop, iterating over the list of names, but something strange happened, and it ended up looking like the third one (below) I can't figure out why it's nesting like that.

\documentclass{minimal}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
\node {texmf}
child { node {doc}}
child { node {fonts}}
child { node {source}}
child { node [selected] {tex}
child { node {generic}}
child { node [optional] {latex}}
child { node {plain}}
}
child [missing] {}
child [missing] {}
child [missing] {}
child { node {texdoc}};
\end{tikzpicture}
\end{document}


\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |-(\tikzchildnode.west)}]
\node {/}
child { node {bin}}
child { node {dev}}
child { node {etc}}
child { node {home}}
child { node {lib}}
child { node {media}}
child { node {mnt}}
child { node {opt}}
child { node {proc}}
child { node {root}}
child { node {sbin}};
\end{tikzpicture}


\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |-(\tikzchildnode.west)}]
\node {/} \foreach \x in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}{
child { node {\x}}};
\end{tikzpicture}


\node{/} \foreach \x in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}{
child { node {\x}}};

\node {/} child [] foreach \name in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}
{node {\name}};


More in Chapter 21 of TikZ Manual Book. Full working example:

\documentclass{minimal}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
grow via three points={one child at (0.5,-0.7) and
two children at (0.5,-0.7) and (0.5,-1.4)},
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]

\node {/} child [] foreach \name in {bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}
{node {\name}};

\end{tikzpicture}
\end{document}

• @Schrödinger'scat I'm too lazy to post a scrennshot, see my name:) – lazyboy Nov 2 at 21:52
• Wow that actually worked, thanks. You can use my screenshot if you want, I think it's identical. – voices Nov 2 at 21:58

This is just to mention that one could use forest. If you really need to wrap it around a list, you may do so, too.

\documentclass[border=3mm]{standalone}
\usepackage[edges]{forest}
\makeatletter
\newcommand{\myforwrapper}[1]{[#1]}
\newcommand{\myfor}[2]{%
\edef#2{}%
\pgfutil@for\my@item:={#1}\do{%
\edef#2{#2\myforwrapper{\my@item}}%
}}
\newcommand{\MyDirectoryTree}[2][]{%
\myfor{#2}{\temp}%
\edef\temp{\noexpand\begin{forest}
for tree={grow'=east,folder,draw}
[/
\temp
]
\noexpand\end{forest}}%
\temp}
\makeatother
\begin{document}
\MyDirectoryTree{bin,dev,etc,home,lib,media,mnt,opt,proc,root,sbin}
\end{document}


• how does that work with files and subdirectories (i.e. more levels)? – voices Nov 20 at 13:17
• @voices You can convert pretty much any input to a hierarchical tree. Yet I do not know which input format you have in mind. – Schrödinger's cat Nov 20 at 13:53