节点的水平对齐(使用直线边缘连接器)

节点的水平对齐(使用直线边缘连接器)

我想要的是有三个节点水平对齐(根 1SA、第一级子节点“0--7 HL”和第二级子节点“各种分布4”),并且具有具有相同 y 坐标的直线连接器,以便它们看起来像一条直线。

梅威瑟:

\RequirePackage{tikz}
\usetikzlibrary{fit, positioning, shapes, trees}
\RequirePackage[edges]{forest}

\documentclass{article}

\begin{document}   
\begin{forest}  
for tree={rounded corners,
grow'=0, l=0, l sep=2em, 
         child anchor=west, anchor=west,  
          parent anchor=east, 
          tier/.pgfmath=level(),
         shape=rectangle, rounded corners, draw=red, align=left, top color=white, bottom color=blue!20, minimum width = 2mm, minimum height = 4mm,
          edge path={\noexpand\path[\forestoption{edge}](!u.parent anchor)|-(.child anchor)\forestoption{edge label};},
          }
[{1SA}, calign=child,  calign child=2
        [{0--7 HL}
            [{\underline{\textbf{Various distributions1}}\\Stayman Takis} ]
            [{\underline{\textbf{Various distributions2}}\\Texas} ]
            [{\underline{\textbf{Various distributions3}}\\Stayman normal, if a single major\\Texas, otherwise} ]
            [{\underline{\textbf{Various distributions4}}\\Pass, with bad suit\\Texas, otherwise} ]
            [{\underline{\textbf{Various distributions5}}\\Pass, 0--4 HL\\Texas M 5--7 HL} ]
            [{\underline{\textbf{Various distributions6}}\\Pass, 0--4 HL\\Texas M 5--7 HL} ]
            [{\underline{\textbf{Various distributions7}}\\Pass} ]
        ] 
    ]
\end{forest}
\end{document}

答案1

要获得水平对齐,calign=child还需要为第二级指定。在下面的代码中,我只是为整个树设置了它。

实际上,代码设置了calign=child edge。即使child/parent anchor与不同,这也会产生所需的结果anchor。(calign=child对齐 anchors,而calign=child edge对齐parent anchorchild anchor。)

我还包括了中间孩子的自动计算:calign child=(n_children()+1)/2。(也许有calign=mid-childcalign=mid-child edge并不是一个坏主意?)

\RequirePackage{tikz}
\usetikzlibrary{fit, positioning, shapes, trees}
\RequirePackage[edges]{forest}

\documentclass{article}

\begin{document}   
\begin{forest}  
for tree={rounded corners,
grow'=0, l=0, l sep=2em, 
         child anchor=west, anchor=west,  
         parent anchor=east, 
         tier/.pgfmath=level(),
         shape=rectangle, rounded corners, draw=red, align=left, top color=white, bottom color=blue!20, minimum width = 2mm, minimum height = 4mm,
         edge path={\noexpand\path[\forestoption{edge}](!u.parent anchor)|-(.child anchor)\forestoption{edge label};},
         calign=child edge, calign child=(n_children()+1)/2,
          }
[{1SA}, 
        [{0--7 HL}, 
            [{\underline{\textbf{Various distributions1}}\\Stayman Takis} ]
            [{\underline{\textbf{Various distributions2}}\\Texas} ]
            [{\underline{\textbf{Various distributions3}}\\Stayman normal, if a single major\\Texas, otherwise} ]
            [{\underline{\textbf{Various distributions4}}\\Pass, with bad suit\\Texas, otherwise} ]
            [{\underline{\textbf{Various distributions5}}\\Pass, 0--4 HL\\Texas M 5--7 HL} ]
            [{\underline{\textbf{Various distributions6}}\\Pass, 0--4 HL\\Texas M 5--7 HL} ]
            [{\underline{\textbf{Various distributions7}}\\Pass} ]
        ] 
    ]
\end{forest}
\end{document}

结果

相关内容