使节点距离相等

使节点距离相等

如何缩小标记为红色的两个子代之间的距离?调整s sep不会减少距离,但会以相等的间距向下推下一代。

在此处输入图片描述

\documentclass{article}

\usepackage[edges]{forest}
\usetikzlibrary{shadows}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering}p{#1}}

\begin{document}
 
\scalebox{0.8}{
\forestset{%
  rect/.append style={rectangle, rounded corners=2pt},
  dir tree switch/.style args={at #1}{%
    for tree={
      fit=rectangle,
    },
    where level=#1{
      for tree={
        folder,
        grow'=0,
      },
      delay={child anchor=north},
    }{},
    before typesetting nodes={
      for tree={
        content/.wrap value={\strut ##1},
      },
      if={isodd(n_children("!r"))}{
        for nodewalk/.wrap pgfmath arg={{fake=r,n=##1}{calign with current edge}}{int((n_children("!r")+1)/2)},
      }{},
    },
  },
}


\begin{forest}
dir tree switch=at 1,
  for tree=
  {
    if level=1{align=center,
            edge path={
         \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-15mm) -| (.child anchor)\forestoption{edge label};
      },
      }{% allow multi-line text and set alignment
        align={@{}C{25mm}@{}
        },
    },
    rect,
    minimum size=2cm,
    draw,
    l sep=7.5mm,
    s sep=2.5mm,
    align=center,
    edge+={ultra thick, rounded corners=2pt},
    ultra thick,
    inner color=white,
    outer color=white,
    rounded corners=2pt,
    drop shadow,
  }
  [AAAA
    [BBBB]
    [CCCC]
    [DDDD
        [EEEE]
        [FFFF]
    ]
    [GGGG]
    [HHHH]
  ]
\end{forest}}

\end{document}

答案1

您需要添加fit=tightDDDD 节点

\documentclass{article}

\usepackage[edges]{forest}
\usetikzlibrary{shadows}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering}p{#1}}

\begin{document}
 
\scalebox{0.8}{
\forestset{%
  rect/.append style={rectangle, rounded corners=2pt},
  dir tree switch/.style args={at #1}{%
    for tree={
      fit=rectangle,
    },
    where level=#1{
      for tree={
        folder,
        grow'=0,
      },
      delay={child anchor=north},
    }{},
    before typesetting nodes={
      for tree={
        content/.wrap value={\strut ##1},
      },
      if={isodd(n_children("!r"))}{
        for nodewalk/.wrap pgfmath arg={{fake=r,n=##1}{calign with current edge}}{int((n_children("!r")+1)/2)},
      }{},
    },
  },
}


\begin{forest}
dir tree switch=at 1,
  for tree=
  {
    if level=1{align=center,
            edge path={
         \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-15mm) -| (.child anchor)\forestoption{edge label};
      },
      }{% allow multi-line text and set alignment
        align={@{}C{25mm}@{}
        },
    },
    rect,
    minimum size=2cm,
    draw,
    l sep=7.5mm,
    s sep=2.5mm,
    align=center,
    edge+={ultra thick, rounded corners=2pt},
    ultra thick,
    inner color=white,
    outer color=white,
    rounded corners=2pt,
    drop shadow,
  }
  [AAAA
    [BBBB]
    [CCCC]
    [DDDD,fit=tight % <----
        [EEEE]
        [FFFF]
    ]
    [GGGG]
    [HHHH]
  ]
\end{forest}}

\end{document}

在此处输入图片描述

答案2

虽然forest这确实是一个不错的软件包。但是,如果您需要对事物进行微调,那么简单的 TikZ 就是一种轻松控制事物的方法。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\a{2}
\def\b{2.2}
\path[nodes={minimum size=15mm,draw,thick,text=red}]
(0,0) node (A) {AAAA}
(0,-\b) node (D) {DDDD}
(\a,-\b)    node (G) {GGGG}
(2*\a,-\b)  node (H) {HHHH}
(-\a,-\b)   node (C) {CCCC}
(-2*\a,-\b) node (B) {BBBB}
(D.center) +(.7*\a,-\b) node (E) {EEEE}
(D.center) +(.7*\a,-2*\b) node (F) {FFFF}
;

\foreach \i in {B,C,D,G,H}
\draw (A.south)--+(0,-.15*\b)-|(\i.north);

\draw (D.south)|-(F.west) (D.south)|-(E.west);
\end{tikzpicture}
\end{document}

相关内容