2

Based on this example, I'm trying to move the first level of the folder structure next to each other (as I have 4 folders containing a lot of files).

Until now, I tried this, but I'm stuck at the indentation of the lower level folders. How can I align them like the directory tree?

\documentclass{scrartcl}
\usepackage[edges]{forest}
\usetikzlibrary{shadows,arrows.meta}
\definecolor{folderbg}{RGB}{124,166,198}
\definecolor{folderborder}{RGB}{110,144,169}
\newlength\Size
\setlength\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);
  },
  file/.pic={%
    \filldraw [draw=folderborder, top color=folderbg!5, bottom color=folderbg!10] (-\Size,.4*\Size+5pt) coordinate (a) |- (\Size,-1.2*\Size) coordinate (b) -- ++(0,1.6*\Size) coordinate (c) -- ++(-5pt,5pt) coordinate (d) -- cycle (d) |- (c) ;
  },
}
\forestset{%
  declare autowrapped toks={pic me}{},
  pic dir tree/.style={%
    for tree={%
      folder,
      font=\ttfamily,
      grow'=0,
      directory,
      fit=band,
    },
    before typesetting nodes={%
      for tree={%
        edge label+/.option={pic me},
      },
    },
  },
  pic me set/.code n args=2{%
    \forestset{%
      #1/.style={%
        inner xsep=2\Size,
        pic me={pic {#2}},
      }
    }
  },
  pic me set={directory}{folder},
  pic me set={file}{file},
}

\begin{document}

\begin{forest}
  pic dir tree,
  where level=0{}{
    directory, 
  },
  where level=1{}{
    file,
  },
  [Level 0,grow'=270,
    [Level 1,grow'=0,forked edges,
        [Level 2forked edges,
            [Level 3]
            [Level 3]
        ]
        [Level 2]
    ]
    [Level 1,grow'=0,
      [Level 2,grow'=0,
      ]
      [Level 2
      ]
      [Level 2, file
      ]
    ]
    [Level 1
        [Level 2]
        [Level 2]
    ]
    [Level 1
    ]
  ]
\end{forest}
\end{document}

At the moment I'm getting this result What I get is this But I'm trying to achieve this What I'm trying to achieve

2
  • Welcome to SE. Please read about Minimal Working Example. As it is, your code isn't compilable so it's harder for people to test it if they're willing to help.
    – Miyase
    Commented Jul 10, 2022 at 12:30
  • 1
    Thank you for the hint, I had a typo in the code formating, not displaying the documentclass. I read the MWE and further simplified the document.
    – Baptiste
    Commented Jul 10, 2022 at 14:54

1 Answer 1

2

Less the calligraphic touches (which could be added, if desired, using the appropriate TikZ library), pic dir tree creates a tree with the target structure.

pic dir tree

As is usual when folder and changes of direction are concerned, the method is a little fiddly (not to mention hacky) in places.

\documentclass[tikz,border=10pt]{standalone}
\usepackage[edges]{forest}
% ateb: https://tex.stackexchange.com/a/705158/ addaswyd o gwestiwn Baptiste: https://tex.stackexchange.com/q/650430/
\usetikzlibrary{shadows,arrows.meta}
\definecolor{folderbg}{RGB}{124,166,198}
\definecolor{folderborder}{RGB}{110,144,169}
\newlength\Size
\setlength\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);
  },
  file/.pic={%
    \filldraw [draw=folderborder, top color=folderbg!5, bottom color=folderbg!10] (-\Size,.4*\Size+5pt) coordinate (a) |- (\Size,-1.2*\Size) coordinate (b) -- ++(0,1.6*\Size) coordinate (c) -- ++(-5pt,5pt) coordinate (d) -- cycle (d) |- (c) ;
  },
}
\forestset{%
  declare autowrapped toks={pic me}{},
  declare dimen={edge adjust}{0pt},
  pic dir tree/.style={%
    grow=-90,
    for descendants={grow'=0},
    for tree={%
      folder,
      font=\ttfamily,
      if n children=0{file}{directory},
    },
    delay={%
      for tree={%
        edge label+/.option={pic me},
      },
    },
    before drawing tree={x/.option=!1.x},
    for children={%
      before drawing tree={%
        edge+/.process={Ow{!u.edge adjust}{shorten <=##1-5pt}},
      },
      before typesetting nodes={%
        replace by={%
          [,
            delay={%
              if n=1{%
                edge+={shorten >=-7.5pt,shorten <=-7.5pt,very thick},
                edge path'/.process={%
                  OORw3 {edge adjust}{!ul.edge adjust}{folder indent} 
                  {%
                    ([yshift=-##1+5pt,xshift=##3].parent anchor) -- ([yshift=-##2+5pt,xshift=##3]!ul.parent anchor)%
                  }%
                },
              }{%
                no edge,
              },
            },
            folder,
            grow'=0,
            edge adjust/.process={Odw2+P{n}{3pt}{##1*##2}},
            s sep/.option=edge adjust,
            before computing xy={l'=7.5pt},
            append,
          ]%
        },
      },
    },
  },
  /handlers/.pic me set/.code={%
    \forestset{%
      \pgfkeyscurrentpath/.style={%
        inner xsep=2\Size,
        pic me={pic {#1}},
      },
    }%
  },
  directory/.pic me set=folder,
  file/.pic me set=file,
}

\begin{document}

\begin{forest}
  pic dir tree,
  [Level 0,
    [Level 1,
        [Level 2
            [Level 3]
            [Level 3]
        ]
        [Level 2]
    ]
    [Level 1,
      [Level 2,
      ]
      [Level 2
      ]
      [Level 2
      ]
    ]
    [Level 1
        [Level 2]
        [Level 2]
    ]
    [Level 1
      []
    ]
  ]
\end{forest}

\end{document}

You must log in to answer this question.

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