我正在尝试缩小图片中 AA 和 A 级之间的距离,我试过l sep
但没有效果。
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows}
\begin{document}
\begin{forest}
basic/.style = {draw, thin, drop shadow, font=\sffamily},
upper style/.style = {basic, rounded corners=6pt, edge+={-Stealth, thin}, fill=gray!10, text width=9em},
lower style/.style = {basic, rounded corners=0pt, edge+={-, line width=.4pt}, fill=black!10, text width=9em},
where level<=2{% upper style,
edge path'={
(!u.parent anchor) -- +(0,-5pt) -| (.child anchor)
},text width=7em
}{%
lower style,
},
where level<=1{%
parent anchor=children,
child anchor=parent,
if={isodd(n_children())}{%
calign=child edge,
calign primary child/.process={
O+nw+n{n children}{(#1+1)/2}
},
}{%
calign=edge midpoint,
},
}{
folder,
grow'=0,
}, [Survey Structure,text width=8em,
[Part I: Introduction,, folder, grow'=0, for children={lower style},text width=10em,
[AA
, folder, grow'=0, for children={upper style},
[A]
[B]
[C]
]
]
]
\end{forest}
\end{document}
答案1
l sep
无法在此处工作。在此树中,文件夹设置为向东扩展 ( grow'=0
),因此有关l
维度的任何设置都只能影响x
维度。
OP 代码中AA
和之间的差距是由于过度使用 key 而产生的。此 key 不仅仅是设置一些值(例如,设置一些值)。它是一种复杂的样式,除其他外,它还会推动节点的子节点来产生文件夹的幻觉。给定,子节点从文件夹节点的东边开始它们的生命;是 key “手动”将它们移动到文件夹节点下方的位置。这就是为什么多次应用会导致意外行为的原因。A
folder
l sep
grow'=0
folder
folder
在下面的代码中,我清理了前导码和树,以便folder
只执行一次(针对除根节点之外的所有节点),同时根据节点的级别为其设置适当的样式。这是在for children
下面的注释中实现的(2)
。
下面的代码注释(1)
处理非文件夹节点和文件夹节点之间的过渡,这是出了名的问题。自动 c 对齐(节点相对于其子节点的位置)在这里不起作用,因为它依赖于设置anchor
,而该设置被 style “劫持” folder
。因此,c 对齐必须手动执行。下面,过渡发生在根节点和其子节点之间,我已将根节点置于所选 ( calign child
) 子节点的正上方。
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows}
\begin{document}
\begin{forest}
basic/.style = {draw, thin, drop shadow, font=\sffamily},
upper style/.style = {basic, rounded corners=6pt, edge+={-Stealth, thin}, fill=gray!10, text width=9em},
lower style/.style = {basic, rounded corners=0pt, edge+={-, line width=.4pt}, fill=black!10, text width=9em},
% (1) manual c-alignment of the root node (change "calign child" to align to a non-first child)
calign=child, calign child=1,
after packing node={
tempdima={max_x("!n={calign_primary_child())}")/2},
for children={s-/.register=tempdima},
},
% (2) Apply "folder", "grow", and the level-specific style to the non-root nodes.
for children={
% level 1
grow'=0, folder,
for children={
% level 2
lower style,
grow'=0, folder,
for descendants={
% level > 2
upper style,
grow'=0, folder,
},
},
},
[Survey Structure, text width=8em,
[Part I: Introduction, text width=10em,
[AA
[A]
[B]
[C]
]
]
]
\end{forest}
\end{document}