4

I want to draw a directory tree, see the code below. I can't get the alignment right in the following areas:

  • The H of Here is text and the T of This is some more text must align.
  • All horizontal arrows should be of equal size. That seems to be the case right now, but changing the text/length of some nodes messes up the arrows.
  • All subnodes should align left; every H of Hello should align. Again seems to be the case right now, but not always so with different text.

Can anyone help me with that? :)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}

\tikzset{
  basic/.style  = {draw, text width=2cm, drop shadow,font=\sffamily,rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center,     fill=green!60,
                   text width=8em},
  level 3/.style = {basic, thin, align=left, fill=pink!60, text width=6.5em}
}

\begin{document}
\begin{tikzpicture}[
  level 1/.style={sibling distance=40mm},
  edge from parent/.style={->,draw},
  >=latex]

\node (c1){Here is text};
\node [below of = c1, xshift=15pt] (c11) {Hello};
\node [below=of c11.west,anchor=west] (c12) {Hello};
\node [below=of c12.west,anchor=west] (c13) {Hello};
\node [below=of c13.west,anchor=west] (c14) {Hello};

\node [below of = c14, xshift=-15pt] (c2) {This is some more text};
\node [below of = c2, xshift=30pt] (c21) {Hello};
\node [below=of c21.west,anchor=west] (c22) {Hello};
\node [below=of c22.west,anchor=west] (c23) {Hello};

\foreach \value in {1,2,3,4}
  \draw[->] (c1.195) |- (c1\value.west);

\foreach \value in {1,2,3}
  \draw[->] (c2.195) |- (c2\value.west);
\end{tikzpicture}
\end{document}
1

I think you will be more comfortable with dirtree package as in Henri's answer.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows}

\begin{document}
\begin{tikzpicture}[>=latex]

\node[anchor=west] (c1) {Here is text};
\node [below right = of  c1.south west] (c11) {Hello};
\node [below=of c11.west,anchor=west] (c12) {Hello};
\node [below=of c12.west,anchor=west] (c13) {Hello};
\node [below=of c13.west,anchor=west] (c14) {Hello};

\node[anchor=west]  (c2) at ([yshift=-9mm]c1.west |- c14) {This is some more text};
\node [below right = of  c2.south west] (c21) {Hello};
\node [below=of c21.west,anchor=west] (c22) {Hello World};
\node [below=of c22.west,anchor=west] (c23) {Hello};

\foreach \value in {1,2,3,4}
  \draw[->] (c1.south west) |- (c1\value.west);

\foreach \value in {1,2,3}
  \draw[->] (c2.south west) |- (c2\value.west);
\end{tikzpicture}
\end{document}

enter image description here

3

Would this dirtree solution fit your needs?

\documentclass{article}
\usepackage{dirtree}
\renewcommand\DTstyle{}
\begin{document}

\dirtree{%
.1 .
.2 Here is text.
.3 Hello.
.3 Hello.
.3 Hello.
.3 Hello.
.2 This is some more text.
.3 Hello.
.3 Hello.
.3 Hello.
}

\end{document}

enter image description here

1

Here is a solution which uses the powerful forest package. I have created two versions. The first uses the package currently on CTAN and in TeX distributions. The second uses the new beta version currently being tested and available on Git Hub. For now, I present the first solution:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{
  dir tree/.style={
    for tree={
      parent anchor=south west,
      child anchor=west,
      anchor=mid west,
      inner ysep=1pt,
      grow'=0,
      align=left,
      if level=1{no edge}{
        edge path={
          \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
        },
      },
      font=\sffamily,
      if n children=0{}{
        delay={
          prepend={[,phantom, calign with current]}
        }
      },
      fit=band,
      before computing xy={
        l=2em
      }
    },
  }
}
\begin{document}
\begin{forest}
  dir tree,
  for tree={
    edge={-Stealth}
  }
  [
    [Here is some text.
      [Short thing.]
      [Longer thing.]
      [{Much, much, much, much, much longer thing.}]
      [Taller thing, font=\Huge]
    ]
    [This is some text.
      [A]
      [B]
      [C]
      [D]
    ]
    [Here is some more text with yet further words.
      [This is an extremely tiresome and long and lengthy and repetitious kind of thing.]
      [This is another extremely tiresome and long and lengthy and repetitious kind of thing.]
      [This is one more boring and extremely tiresome and long and lengthy and repetitious kind of thing.]
      [This is disappointingly similar to previous extremely tiresome and long and lengthy and repetitious kinds of things.]
    ]
  ]
\end{forest}
\end{document}

directory tree with current <code>forest</code>

  • Code for the new version is in the forest testing chat room ;). – cfr Nov 12 '15 at 0:48

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.