2

I am struggling with reducing the vertical space of a directory tree using the forest class. My code is based on this SO-Answer (https://tex.stackexchange.com/a/328890/32924). Although, I added inner ysep=0pt for the styling of the tree and the styling of the file the resulting tree is unevenly spaced (see image).

Moreover, I would like to ask you if you have any idea on how to remove the root element (i.e. system) of the directory tree and show config, lib, and files as root elements of the directory.

enter image description here

The code I am using is:

\begin{document}
 \begin{forest}
      for tree={
        font=\ttfamily,
        grow'=0,
        child anchor=west,
        parent anchor=south,
        anchor=west,
        calign=first,
        inner xsep=7pt,
        inner ysep=0pt,
        edge path={
          \noexpand\path [draw, \forestoption{edge}]
          (!u.south west) +(7.5pt,0) |- (.child anchor) pic {folder} \forestoption{edge label};
        },
        % style for your file node 
        file/.style={edge path={\noexpand\path [draw, \forestoption{edge}]
          (!u.south west) +(7.5pt,0) |- (.child anchor) \forestoption{edge label};},
          inner xsep=2pt,inner ysep=0pt,font=\small\ttfamily
                     },
        before typesetting nodes={
          if n=1
            {insert before={[,phantom]}}
            {}
        },
        fit=band,
        before computing xy={l=15pt},
      }  
    [/
      [config
      ]
      [lib
        [Access
        ]
        [file.txt,file
        ]
      ]
      [file2.txt,file
      ]
      [gzipfile.txt,file
      ]
      [gzipfile2.txt,file
      ]
    ]
 \end{forest}
 \end{document}
2
  • 1
    Welcome to TeX.SE!
    – Mensch
    Commented Nov 10, 2022 at 20:45
  • Welcome to TeX.SX. When you post a question, please always provide a "Minimal Working Example" (MWE) that starts with \documentclass, includes all relevant \usepackage commands, ends with \end{document} and compiles without errors, even if it does not produce your desired output.
    – Sandy G
    Commented Nov 11, 2022 at 0:10

1 Answer 1

3

To decrease the vertical spacing you need to change s sep, since the folder format has siblings organized vertically. The image below has s sep=2pt, but you can make it smaller (or even negative, but that will overlap things). To keep the spacing even you need your cells to have consistent height, so I set minimum height=3mm. No need to change inner y sep.

enter image description here

I'm not sure what you want for the final look without the system node. I removed it, but I don't think that's what you wanted. Perhaps ask a new question with a sketch of your desired output.

\documentclass{article}

\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,
        %inner ysep=0pt,
        s sep=2pt,
        minimum height=3mm,
        edge path={
          \noexpand\path [draw, \forestoption{edge}]
          (!u.south west) +(7.5pt,0) |- (.child anchor) pic {folder} \forestoption{edge label};
        },
        % style for your file node 
        file/.style={edge path={\noexpand\path [draw, \forestoption{edge}]
          (!u.south west) +(7.5pt,0) |- (.child anchor) \forestoption{edge label};},
          inner xsep=2pt,inner ysep=0pt,font=\small\ttfamily
                     },
        before typesetting nodes={
          if n=1
            {insert before={[,phantom]}}
            {}
        },
        fit=band,
        before computing xy={l=15pt},
      }  
    [
      [config
      ]
      [lib
        [Access
        ]
        [file.txt,file
        ]
      ]
      [file2.txt,file
      ]
      [gzipfile.txt,file
      ]
      [gzipfile2.txt,file
      ]
    ]
 \end{forest}
 \end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .