0

I am using dirtree to generate the tree structure for a directory.

The problem is I cannot use special characters such as the underscore (_) or the dash (-) between words of a file or directory name.

For example, here's a brief tree showing what I'm trying to achieve.

  /
  |
  |--- [node_modules]
  |--- package-lock.json
Marijn
  • 37,699

1 Answers1

3

The issue is the _ in node_modules, this needs to be escaped. That is not special for dirtree, the _ is the math mode subscript command in LaTeX, so it always needs to be escaped when you want to write a literal underscore.

MWE:

\documentclass{article}
\usepackage{dirtree}
\begin{document}
\dirtree{%
.1 /.
.2 bin.
.2 home.
.3 [node\_modules].
.4 package-lock.json.
}
\end{document}

Result:

enter image description here


Alternative: a directory tree with forest, taken from Making a directory tree of folders and files. Code is a bit longer. Note that here you need to escape the [ and ] characters as well, by putting them in a group with { and } (otherwise forest thinks they close the node).

\documentclass{article}
\usepackage[edges]{forest}
\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,
    },
    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{}{% folder icons by default; override using file for file icons
    directory,
  },
  [system
    [config
    ]
    [lib
      [Access
      ]
      [{[node\_modules]}
      ]
      [package-lock.json, file
      ]
    ]
    [templates
    ]
    [tests
    ]
  ]
\end{forest}
\end{document}

enter image description here

Marijn
  • 37,699