我遵循了这篇文章的基于森林的解决方案制作(简单)目录树创建目录结构。我想在每个文件和名称的右侧添加其完整路径。完整路径也应左对齐。我想出了这个肮脏的解决方案:
\documentclass{standalone}
\usepackage{forest}
\usetikzlibrary{positioning}
\begin{document}
\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
s sep=6pt,
inner sep=0pt,
edge path={
\noexpand\path [draw, \forestoption{edge}]
(!u.south west) +(6pt,0) |- (.child anchor)\forestoption{edge label};
},
before typesetting nodes={
if n=1
{insert before={[,phantom]}}
{}
},
fit=band,
before computing xy={l=5mm},
}
[a,name=a
[b
[c
[d]
]
]
[e
[f]
[b]
]
]
%
\node[right of=a,node distance=2cm] (desc) {\tt /a};
\node[below=13pt of desc.west,anchor=west] (desc) {\tt /a/b};
\node[below=13pt of desc.west,anchor=west] (desc) {\tt /a/b/c};
\node[below=13pt of desc.west,anchor=west] (desc) {\tt /a/b/c/d};
\node[below=12pt of desc.west,anchor=west] (desc) {\tt /a/e};
\node[below=13pt of desc.west,anchor=west] (desc) {\tt /a/e/f};
\node[below=13pt of desc.west,anchor=west] (desc) {\tt /a/e/b};
\end{forest}
\end{document}
显示如下:
看起来还不错,但我想提出一个干净的解决方案,避免对完整路径之间的距离进行硬编码。
答案1
我建议通过使用forked edge
和 来简化您的树fork sep=0pt
。(注意,这需要\usepackage[edges]{forest}
。)我将每个路径描述作为一个新的子节点,然后用于if n children=0{no edge, tier=desc}{l-=4mm}
防止绘制最后的边并使它们在维度上保持对齐l
(这是tier
使用的)。如果n children
≠0,则l
维度缩短4mm
。如果您希望路径描述更接近树,您也可以缩短(相同或不同的量)。
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
font=\ttfamily,
grow'=0,
anchor=west,
calign=first,
s sep=6pt,
inner sep=0pt,
forked edge,
fork sep=0pt,
l sep=3mm,
if n children=0{no edge, tier=desc}{l-=4mm}
}
[a
[/a]
[b
[/a/b]
[c
[/a/b/c]
[d
[/a/b/c/d]
]
]
]
[e
[/a/e]
[f
[/a/e/f]
]
[b
[/a/e/b]
]
]
]
\end{forest}
\end{document}