1

I need to typeset a directory tree, and I found here an appropriate solution using forest package. After a slight modification (the squares on the paths, as suggested here), I got this:

what I got

Unfortunately, as you can see, the spacing between elements is far from perfect. Take, for example, the vertical distance between text 5.1 and text 6, and that between text 6 and text 5.2: they are (significantly and dreadfully) different, which I don't accept.

Is there a way to solve this problem, that is, to have all elements spaced correctly?


The following is the code for the tree in the picture:

\begin{forest} dir tree,
for tree = {font=\ttfamily}
[text 1
    [text 2
        [text 3
            [text 4
                [text 5.1
                    [text 6]
                ][text 5.2
]]]]]
\end{forest}

And these are forest settings:

\usepackage{forest}
\forestset{
  dir tree/.style={
    for tree={
      parent anchor=south west,
      child anchor=west,
      anchor=mid west,
      inner ysep=0pt,
      grow'=0,
      align=left,
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) ++(1em,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
      },
      if n children=0{}{
        delay={
          prepend={[,phantom, calign with current]}
        }
      },
      fit=band,
      before computing xy={
        l=2em
      }
    },
  }
}
  • What if the distance is really similar, but not identical to the decimal? – Alenanno Jan 05 '16 at 12:04
  • I'm sorry, @Alenanno, I don't understand what you mean... – 80shirts Jan 05 '16 at 12:09
  • I'm measuring the distance between the three elements, and one distance is 22.34, the other is 22.14. So a 0.2 difference in distance. – Alenanno Jan 05 '16 at 12:10
  • @Alenanno Unfortunately in my diagram the pieces of text are longer, so the issue with the vertical spacing is more visible... – 80shirts Jan 05 '16 at 13:09
  • Please post complete examples i.e. the complete code required to reproduce a problem, when asking questions as it makes things much easier for helpers and anybody interested in the topic of the question, even if the ideas are new to them. – cfr Jan 05 '16 at 22:08
  • @cfr I'm sorry, I thought that the code I posted was sufficient. Anyway, your answer perfectly solved my problem. I'll need to study it a bit, to better understand the changes that you made, but it works. So thanks! – 80shirts Jan 05 '16 at 22:19
  • 1
    It is better if you post something which can be copy-paste-compiled. Especially for people less familiar with the packages etc. (Also, it is just easier generally.) If you want to understand it, two things may help. One is to add draw to the nodes of your original tree. The other is to add x, draw in place of , phantom. It makes it easier to see what is happening. (At least, that's what I did.) – cfr Jan 05 '16 at 22:23

1 Answers1

3

Maybe something like this?

modified dir tree

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\forestset{
  dir node/.style={
    parent anchor=south west,
    child anchor=west,
    anchor=west,
    inner ysep=0pt,
    align=left,
  },
  dir tree/.style={
    for tree={
      grow'=0,
      dir node,
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) ++(1em,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
      },
      if n children=0{}{
        delay={
          prepend={[text 1, dir node, phantom, calign with current]}
        }
      },
      fit=band,
      before computing xy={
        l=2em,
      }
    },
  }
}
\begin{document}
\begin{forest}
  dir tree,
  for tree = {
    font=\ttfamily
  }
  [text 1
    [text 2
      [text 3
        [text 4
          [text 5.1
            [text 6]
          ]
          [text 5.2
          ]
        ]
      ]
    ]
  ]
\end{forest}
\end{document}
cfr
  • 198,882