我正在使用此代码来显示目录树,效果很好:
\definecolor{treelines}{RGB}{40, 63, 100}
\definecolor{treefirst}{RGB}{40, 63, 100}
\definecolor{treesec}{RGB}{55, 84, 136}
\begin{forest}
for tree={
text=white,
text width=2.3cm,
minimum height=0.75cm,
if level=0
{fill=treefirst}
{fill=treesec},
rounded corners=4pt,
grow'=0,
child anchor=west,
parent anchor=south,
anchor=west,
calign=first,
edge={treelines,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},
}
[Project
[item1]
[item2]
[item3
[sitem1]
[sitem2]
[sitem3]
[sitem4]
]
]
\end{forest}
输出结果如下:
现在我想让 item3 向东延伸,如下所示:
我无法让它工作。使用 calign = center 至少可以为子树生成正确的格式,但边缘仍然全部错误。
答案1
如果需要半自动解决方案...
请注意,下面使用库folder
中的样式edges
而不是自己的样式。
\documentclass[tikz,border=10pt]{standalone}
\usepackage[edges]{forest}
\definecolor{treelines}{RGB}{40, 63, 100}
\definecolor{treefirst}{RGB}{40, 63, 100}
\definecolor{treesec}{RGB}{55, 84, 136}
\forestset{% ateb: https://tex.stackexchange.com/a/705018/ addaswyd o gwestiwn flewz: https://tex.stackexchange.com/q/531154/
dir down/.style={%
for tree={%
folder,
grow'=0,
},
},
dir switch/.style={%
delay={%
prepend={%
[,
grow'/.option=!r.grow,
folder,
no edge,
china,
delay n=2{%
coordinate,
for nodewalk={%
while nodewalk valid={next}{append=!next,do dynamics}%
}{},
},
before typesetting nodes={%
if={%
> Ow+P {n children}{isodd(##1)}
}{%
for nodewalk={fake=first,until={ > Ow+n O= {!u.n children}{(##1+1)/2}{n} }{fake=next},current}{calign with current edge,edge+={rounded corners=false}}
}{%
for nodewalk={fake=first,until={ > Ow+n O= {!u.n children}{(##1)/2}{n} }{fake=next},current}{%
insert after={%
[, phantom,calign with current,coordinate,edge+={rounded corners=false}]
},
},
},
for children={edge path'={(!uu.parent anchor) -- ++(\foresteregister{folder indent},0pt) |- (.child anchor)}},
},
]%
},
},
before typesetting nodes={
grow'=#1,
parent anchor/.option=!r.grow,
l sep'=0pt,
s sep'=0pt,
},
},
china/.style={%
for tree={%
text=white,
text width=2.3cm,
minimum height=0.75cm,
if level=0{%
fill=treefirst,
}{%
fill=treesec,
},
rounded corners=4pt,
edge={treelines,rounded corners,line width=1pt},
},
},
}
\begin{document}
\begin{forest}
dir down,
china,
[Project
[item1]
[item2]
[item3, dir switch=90
[sitem1]
[sitem2]
[sitem3]
[sitem4]
]
]
\end{forest}
\end{document}