包装森林:父子水平对齐

包装森林:父子水平对齐

这是我的森林:

在此处输入图片描述

我如何水平Family1A very long long long ... text齐其子项?

如果我可以在这里问另一个问题,我该如何将子节点的文本左对齐?我只希望节点内的文本居中对齐Domain,而子节点内的文本左对齐。目前它们都是。SubdomainFamilytext centered

代码:

\forestset{
  no border/.style={
    for tree={
      edge path={
        \noexpand\path [draw, thin, \forestoption{edge}] (!u.parent anchor) +(5pt,0) (.child anchor)\forestoption{edge label}; 
      },
    },
  },
  border/.style={
    for tree={
      edge path={
        \noexpand\path [draw, thin, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label}; 
      },
      draw,
      thin,
    }
  },
  domain/.style={
    text width=3cm,
    parent anchor=south,
    rotate=90,
    fill=black, text=white
  },
  subdomain/.style={
    text width=3cm,
    parent anchor=south,
    rotate=90
  },
  family/.style={
    text width=3cm,
    parent anchor=east,
    tier=family,
    calign=child,
    calign primary child=1
  },
}

\begin{figure}[!ht]
  \begin{forest}
    for tree={
      parent anchor=east,
      grow'=east,
      text centered,
      minimum width=2cm,
      text width=9.8cm
    }
    [Domain, border, domain
      [Subdomain, subdomain
        [Family1, family
          [A very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text]
        ]
        [Family2, family
          [A very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text]
        ]
        [Family3, family
          [A very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text]
        ]
      ]
    ]
  \end{forest}
\end{figure}

答案1

如果我正确理解了您的第一个问题,您想要水平居中对齐。

所讨论的节点(FamilyX 及其子节点)已经水平对齐(由于calign设置)。更准确地说,它们的锚点是。默认情况下,Forest 将节点锚点设置为base。对于多行节点,基点位于文本的第一行。因此,父节点(FamilyX)和子节点(很长很长...)的第一行是对齐的。

要(水平)对齐节点的中心,请对它们两个都说anchor=east(或center,或)。west

对于第二个问题,使用tikzkey align,例如/tikz/align=flush left。您需要指定路径(/tikz/),因为您不想调用Forest的align密钥。

完整代码:

\documentclass{article}

\usepackage{forest}

\begin{document}

\forestset{
  no border/.style={
    for tree={
      edge path={
        \noexpand\path [draw, thin, \forestoption{edge}] (!u.parent anchor) +(5pt,0) (.child anchor)\forestoption{edge label}; 
      },
    },
  },
  border/.style={
    for tree={
      edge path={
        \noexpand\path [draw, thin, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label}; 
      },
      draw,
      thin,
    }
  },
  domain/.style={
    text width=3cm,
    parent anchor=south,
    rotate=90,
    fill=black, text=white
  },
  subdomain/.style={
    text width=3cm,
    parent anchor=south,
    rotate=90
  },
  family/.style={
    text width=3cm,
    parent anchor=east,
    tier=family,
    calign=child,
    calign primary child=1,
    anchor=east,
    for children={
      /tikz/align=flush left,
      anchor=east,
    }
  },
}

\begin{figure}[!ht]
  \begin{forest}
    for tree={
      parent anchor=east,
      grow'=east,
      text centered,
      minimum width=2cm,
      text width=9.8cm,
    }
    [Domain, border, domain
      [Subdomain, subdomain
        [Family1, family
          [A very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text]
        ]
        [Family2, family
          [A very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text]
        ]
        [Family3, family
          [A very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text]
        ]
      ]
    ]
  \end{forest}
\end{figure}

\end{document}

相关内容