森林:如何将父节点水平置于其子节点之间?

森林:如何将父节点水平置于其子节点之间?

下列的这个答案,我不明白为什么父节点与其子节点的西边对齐而不是中心对齐,尽管设置了

parent anchor=south,
child anchor=north,
calign=center,

我的问题是如何正确地使每个父级在其子级之间水平居中?

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
    {fill=#1},
    for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
    root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
  forked edges,
  for tree={%
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
    parent anchor=south,
    child anchor=north,
    calign=center
  },
  where={level()<=1}{%
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=2pt,% length of edge to grand child
     if level=2{%
       !u.s sep'+=10pt,
       edge path'={%
         (!u.parent anchor) -- (.child anchor)
       },
     }{},
   },
      [Grandparent,root color={brown}
        [Parent 1, parent color={red}
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2,parent color={green}
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3,parent color={yellow}
            [Child 3
                [Grandchild 3]
            ]
        ]
    ]
\end{forest}
\end{document}

在此处输入图片描述

答案1

请注意,您没有询问如何做任何事情。您要求对对齐进行解释。Sašo 的回答回答了这个问题。

我从你的评论这就是你意味着在您的问题正文中询问的是您在主题行中实际询问的内容,即如何避免您的问题中描述的效果。

此时重新开始可能会更简单。但是,根据 Alan Munn 对我的代码的修改,您可以执行以下操作。

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
    {fill=#1},
    for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
    root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
  forked edges,
  for tree={%
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
    parent anchor=children,
    child anchor=parent,
  },
  where={level()<=1}{%
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=2pt,% length of edge to grand child - NO IT IS NOT!!!
     if level=2{%
       !u.s sep'+=10pt,
       anchor=north,
       child anchor=north,
       edge path'={%
         (!u.parent anchor) -- (.child anchor)
       },
     }{
       if level=3{
         before computing xy={l=14pt-((\textwidth-6*\gap)/6)},
       }{}
     },
   },
      [Grandparent,root color={brown}
        [Parent 1, parent color={red}
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2,parent color={green}
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3,parent color={yellow}
            [Child 3
                [Grandchild 3 [Great-Grandchild]]
            ]
        ]
    ]
\end{forest}
\end{document}

处于 2 级中心的儿童

或者,改编我的代码版本,从family treebranch shade创建family tree toobranch shading,我们可以写下以下内容。

\documentclass[border=10pt,multi,tikz,dvipsnames,svgnames,rgb]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\forestset{
  declare dimen register=gap,
  gap'=10mm,
  declare count register=twist,
  twist'=2,
  family tree too/.style={
    forked edges,
    for tree={
      rounded corners,
      minimum width/.wrap pgfmath arg={##1}{(\textwidth-6*(gap))/3},
      minimum height=4ex,
      edge={-Latex},
      font=\sffamily,
      text centered,
      blur shadow,
      edge=thick,
    },
    where={level()<(twist)}{%
      parent anchor=children,
      l sep+=5pt,
    }{%
      folder,
      grow'=0,
      if={level()==(twist)}{%
        before typesetting nodes={child anchor=north},
        anchor=north,
        edge path'={%
          (!u.parent anchor) -- (.child anchor)
        },
      }{
        l sep'+=2pt,
        if level=3{
          before computing xy={
            l/.wrap pgfmath arg={##1}{14pt-((\textwidth-6*(gap))/6)}
          },
        }{}
      },
    },
  },
  branch shading/.style args={from #1 to #2}{
    before typesetting nodes={
      tempcountc/.max={level}{current,tree},
      tempcountb/.option=level,
      tempcounta=(tempcountc)-(tempcountb)+1,
      temptoksa/.option=name,
      TeX/.wrap pgfmath arg={
        \colorlet{##1col1}{#1}
        \colorlet{##1col2}{#2}
      }{name()},
      for tree={
        rounded corners,
        top color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
        bottom color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        draw/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        thick,
      },
      +edge=darkgray,
      for descendants={
        +edge/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
      },
    }
  },
}
\begin{document}
\begin{forest}
  family tree too,
   [Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
     [Parent 1, branch shading=from cyan to blue
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shading=from Pink to WildStrawberry
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shading=from SpringGreen to ForestGreen
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\begin{forest}
  family tree too,
   [Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
     [Parent 1, branch shading=from blue!80 to blue!20
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shading=from WildStrawberry!80 to WildStrawberry!20
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shading=from ForestGreen!80 to ForestGreen!20
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\end{document}

其他可能性

答案2

样式文件夹设置了 calign 等。

还要注意,在文件夹节点处,树实际上向东生长!

相关内容