垂直对齐 Tikz 节点并让箭头正确工作

垂直对齐 Tikz 节点并让箭头正确工作

我想垂直对齐 Tikz 树的兄弟节点,使它们的顶部(而不是中心)处于同一水平。我尝试使用该选项执行此anchor=north操作(请参阅下面的 MWE),但这样做会弄乱箭头。我希望箭头能够汇聚在一点,就像没有该anchor=north选项的 MWE 一样。

代码对齐但箭头不正确:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,trees,calc}
\begin{document}


\begin{tikzpicture}[every node/.style=%
  {draw, rectangle}, edge from parent path={ (\tikzparentnode) |-
    ($(\tikzparentnode)!0.5!(\tikzchildnode)$) -| (\tikzchildnode)},%
  level distance=10\baselineskip,%
  text width=3cm, %
  text centered, %
  sibling distance=13em,%
  anchor=north, %
  edge from parent/.style={draw,line width=1pt,<-}%
  ]%
%
  \node {some text} %
  child {node {little text.}  %
  } %
  child {node {Text Text Text Text Text Text Text Text
      Text Text Text Text Text Text Text Text Text Text Text Text Text
      Text Text}}%
  ;
\end{tikzpicture}

\end{document}

输出:

在此处输入图片描述

答案1

parent如果为和节点指定锚点,似乎可以工作child

代码输出

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,trees,calc}
\begin{document}
\begin{tikzpicture}[
  every node/.style={
    draw,
    rectangle,
    anchor=north,
    text width=3cm, 
    text centered,
  },
  edge from parent path={
    (\tikzparentnode.south) |-
    ($(\tikzparentnode.south)!0.5!(\tikzchildnode.north)$) -|
    (\tikzchildnode.north)
  },
  edge from parent/.style={draw,line width=1pt,<-},
  level distance=10\baselineskip,
  sibling distance=13em,
  ]

  \node {some text} 
  child {node {little text.}  
  } 
  child {node {Text Text Text Text Text Text Text Text
      Text Text Text Text Text Text Text Text Text Text Text Text Text
      Text Text}}
  ;
\end{tikzpicture}

\end{document}

答案2

withforest包更简单...

\documentclass[border=3mm,
           preview]{standalone}
\usepackage[edges]{forest}

\begin{document}
\tikzset{every label/.style={xshift=-4ex, text width=6ex, align=right, inner sep=1pt,
                         font=\footnotesize, text=red}}
    \begin{forest}
    for tree={              % style of tree nodes
                    draw, semithick,
       text width = 32mm, text badly centered,
        inner sep = 1mm,
                            % style of tree (edges, distances, direction)
             edge = {draw, semithick, stealth-},
    parent anchor = south,
     child anchor = north,
             grow = south,
    forked edge,            % for forked edge
            l sep = 12mm,   % level distance
         fork sep = 6mm,    % distance from parent to branching point
                }
    [some text
      [little text.]
      [Text Text Text Text Text Text Text Text
       Text Text Text Text Text Text Text Text
       Text Text Text Text Text Text Text]
    ]
    \end{forest}

在此处输入图片描述

相关内容