制作(简单)目录树

制作(简单)目录树

我正在尝试用 LaTeX 创建一个简单的目录树表示。类似于:

spam
 |
 +-- ham
 +-- eggs
     |
     +-- more spam
     +-- dead parrots

树不会那么长(比如说,10-15 个条目左右),有 3 个左右的级别,而且是一次性的事情。所以,欢迎快速而粗略的解决方案。推荐时tikz1,请注意,我几乎没有这方面的经验,所以我很感激一个干净简单的解决方案,即使它看起来不太完美。

1Tex.SEtikz几乎就像jqueryStackOverflow 上反复出现的笑话。


编辑:根据 Thorsten 的建议,目录树是一个不错且易于使用的解决方案。上面的例子可以写成:

\usepackage{dirtree}
...
\dirtree{%
.1 spam.
.2 ham.
.2 eggs.
.3 more spam.
.3 dead parrots.
}

答案1

目录树包裹是你的朋友。

答案2

这是一个使用强大的forest包;由于它建立在 TikZ 之上,因此定制的可能性非常大。

例如,人们可以轻松模拟以下风格dirtree

\documentclass[border=5pt]{standalone}
\usepackage{forest}

\begin{document}

\begin{forest}
  for tree={
    font=\ttfamily,
    grow'=0,
    child anchor=west,
    parent anchor=south,
    anchor=west,
    calign=first,
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }
[text1
  [text1.1
    [text1.1.1]
    [text1.1.2]
    [text1.1.3]
  ]
  [text1.2
    [text1.2.1]
    [text1.2.2]
  ]
]
\end{forest}

\end{document}

在此处输入图片描述

对之前的规格进行了简单的修改

  for tree={
    font=\sffamily,
    text=white,
    text width=2cm,
    minimum height=0.75cm,
    if level=0
      {fill=mygreenii}
      {fill=mygreeni},
    rounded corners=4pt,
    grow'=0,
    child anchor=west,
    parent anchor=south,
    anchor=west,
    calign=first,
    edge={mygreenii,rounded corners,line width=1pt},
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    s sep=15pt,
    before computing xy={l=15pt},
  }

现在给出:

在此处输入图片描述

初始主题也可以轻松更改为

\documentclass[border=5pt]{standalone}
\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,
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- (.child anchor) pic {folder} \forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }  
[system
  [config
  ]
  [lib
    [Access
    ]
    [Plugin
    ]
  ]
  [templates
  ]
  [tests
  ]
]
\end{forest}

\end{document}

产生了

在此处输入图片描述

将这些设置与变体相结合my answer使用文件夹图标绘制目录树可以生产

\documentclass[border=10pt]{standalone}
\usepackage{forest}

\definecolor{fblue}{RGB}{92,144,192}
\definecolor{fgreen}{RGB}{34,162,70}

\newcommand\myfolder[2][fblue]{%
\begin{tikzpicture}[overlay]
\begin{scope}[xshift=20pt]
\filldraw[rounded corners=1pt,fill=#1,draw=white,double=black]
  (-23pt,10pt) -- ++(3pt,5pt) -- ++(18pt,0pt) -- ++(40:3pt) -- ++(9pt,0pt) -- ++(-40:3pt)
  -- (20pt,15pt) -- (23pt,10pt) -- cycle;
\filldraw[rounded corners,draw=white,double=black,top color=#1,bottom color=#1!30]
  (-22pt,-12pt) -- ++(44pt,0pt) -- (25pt,12pt) coordinate (topr) -- ++(-50pt,0pt) coordinate (topl) -- cycle;
\end{scope}  
\end{tikzpicture}%
\makebox[35pt]{\raisebox{-3pt}{{\ttfamily/#2}}}%
}

\begin{document}

\begin{forest}
  for tree={
    font=\sffamily,
    minimum height=0.75cm,
    rounded corners=4pt,
    grow'=0,
    inner ysep=8pt,
    child anchor=west,
    parent anchor=south,
    anchor=west,
    calign=first,
    edge={rounded corners},
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(12.5pt,0) |- (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom,minimum height=18pt]}}
        {}
    },
    fit=band,
    s sep=12pt,
    before computing xy={l=25pt},
  }
[\myfolder{}
  [\myfolder{bin}]
  [\myfolder{dev}]
  [{\myfolder[fgreen]{home}}
    [{\myfolder[fgreen]{anna}}]
    [{\myfolder[fgreen]{tess}}]
  ]
  [\myfolder{media}]
  [\myfolder{mnt}]
  [\myfolder{usr}]
]
\end{forest}

\end{document}

在此处输入图片描述

这里要注意的是,一旦建立了垂直结构的基本规范(我借用了并做了一些修改),成本加运费(例如,参见her answer如何调整森林环境中的大量节点?)):

  for tree={
    grow'=0,
    child anchor=west,
    parent anchor=south,
    anchor=west,
    calign=first,
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }

人们所要做的就是让想象力飞翔,并利用/TikZ 的力量产生尽可能多的变化forest

答案3

按照@cfr的指导:重写其中一个贡萨洛·梅迪纳的树但也可以选项将文件放入树中。只需调用is file文件节点的选项即可。此外,文件夹pic现在接受一个参数,即文件夹图标大小,可以通过调用来强调特定文件夹this folder size=<dim>。键folder tree indent=<dim>设置多少图标进入树内并folder indent=<dim>设置图标后线来自。

样式folder icons(或)是在样式folder icons=<dim>的顶部定义的,对其所做的唯一更改是路径(将 a 放在末尾)。folderforestedgepic重要的:正如 @cfr 在评论中提到的那样,不使用它grow'=0会导致意外的结果。这是因为图标放在边缘的末端,导致图标和文本重叠,为了解决这个问题,inner xsep会自动添加一些额外的空间folder icons,但如果树垂直生长,则需要xsep变成ysep。请注意,使用 justsep可以解决这两种情况,但边缘离文本太远,使其不那么漂亮......

\documentclass[tikz,border=5mm]{standalone}
\usepackage[edges]{forest}

\definecolor{foldercolor}{RGB}{124,166,198}

\tikzset{pics/folder/.style={code={%
    \node[inner sep=0pt, minimum size=#1](-foldericon){};
    \node[folder style, inner sep=0pt, minimum width=0.3*#1, minimum height=0.6*#1, above right, xshift=0.05*#1] at (-foldericon.west){};
    \node[folder style, inner sep=0pt, minimum size=#1] at (-foldericon.center){};}
    },
    pics/folder/.default={20pt},
    folder style/.style={draw=foldercolor!80!black,top color=foldercolor!40,bottom color=foldercolor}
}

\forestset{is file/.style={edge path'/.expanded={%
        ([xshift=\forestregister{folder indent}]!u.parent anchor) |- (.child anchor)},
        inner sep=1pt},
    this folder size/.style={edge path'/.expanded={%
        ([xshift=\forestregister{folder indent}]!u.parent anchor) |- (.child anchor) pic[solid]{folder=#1}}, inner ysep=0.6*#1},
    folder tree indent/.style={before computing xy={l=#1}},
    folder icons/.style={folder, this folder size=#1, folder tree indent=3*#1},
    folder icons/.default={12pt},
}

\begin{document}
  \begin{forest}
    for tree={font=\sffamily, %grow'=0,
    folder indent=.9em, folder icons,
    edge=densely dotted}
    [main folder
      [images, this folder size=20pt
          [wallpapers]
          [logo.pdf, is file]]
      [tex-files
          [chapter1.tex, is file]]
      [main.tex, is file]
      [main.aux, is file]
    ]
  \end{forest}
\end{document}

当 grow'=0

现在删除grow'=0并替换xsepysep(代码中只有一个实例 - 也folder indent需要调整):

垂直增长 - 需要 ysep 而不是 xsep

main.tex靠近边缘是因为fit设置为tight,将其设置为可以band解决问题,但树会长得更大......

答案4

重写其中一个上面的 Gonzalo Medina 的树适用于 Forest v2,这使得绘制这种树变得更加容易。

其他两种样式的更新版本:https://tex.stackexchange.com/a/361248/https://tex.stackexchange.com/a/328987/

\documentclass[tikz,border=10pt,multi,dvipsnames,svgnames,x11names]{standalone}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
  my label/.style={
    label={[font=\sffamily]right:{#1}},
  },
  for tree={% addaswyd o gôd Gonzalo Medina: https://tex.stackexchange.com/a/270761/
    folder,
    font=\sffamily,
    text=white,
    minimum height=0.75cm,
    text width=20mm,
    if level=0{fill=ForestGreen}{fill/.wrap pgfmath arg={SlateBlue#1}{int(4-(mod((level()-1),4)))}},
    rounded corners=4pt,
    grow'=0,
    edge={ForestGreen,rounded corners,line width=1pt},
    fit=band,
  },
  [text1
    [text1.1
      [text1.1.1
        [text1.1.1.1
          [text1.1.1.1.1]
          [text1.1.1.1.2]
          [text1.1.1.1.3]
        ]
      ]
      [textt1.1.2
        [text1.1.2.1]
      ]
      [text1.1.3
        [text1.1.3.1]
      ]
    ]
    [text1.2
      [text1.2.1]
      [text1.2.2]
    ]
  ]
\end{forest}
\end{document}

GM 树 v2 版更新

相关内容