适合树的每个树枝的长度

适合树的每个树枝的长度

我发现这棵树的例子

http://www.texample.net/tikz/examples/tree/

我曾经用它来建造自己的树,但是我遇到了一些问题,因为所有树枝的长度都相同,因此导致一些树叶重叠。

如图所示:

% A simple Tree
% Author: Stefan Kottwitz
% https://www.packtpub.com/hardware-and-c ... x-cookbook
\documentclass[border=10pt]{standalone} 
%%%<
\usepackage{verbatim}
%%%>
\begin{comment}
:Title: A simple Tree
:Tags: Trees;Cookbook
:Author: Stefan Kottwitz
:Slug: tree

A simple tree with a style for all nodes.
\end{comment}

\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[sibling distance=15em,
every node/.style = {shape=rectangle, rounded corners,
draw, align=center,
top color=white, bottom color=blue!20}]]
\node {Sentence}
child { node {W1} 
child { node {M1} 
child { node {P1}}
child { node {P2}}}
child { node {M2} 
child { node {P1}}
child { node {P2}}}}
child { node {W2} 
child { node {M1}
child { node {P1} }}}
child { node {W3} 
child { node {M1} }
child { node {M2} }};

\end{tikzpicture}
\end{document}

创建此图像。

在此处输入图片描述 如何修复重叠的叶子,或者如何单独设置树枝的长度......

答案1

两个版本:带tikz树和带forest

\documentclass[tikz, margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
    \begin{tikzpicture}[
every node/.style = {shape=rectangle, rounded corners, draw,
                     top color=white, bottom color=blue!20},
   level distance = 4em,
   level 1/.style = {sibling distance=24mm},
   level 2/.style = {sibling distance=16mm},
   level 3/.style = {sibling distance=8mm},
                        ]
\node {Sentence}
    child { node {W1}
        child { node {M1}
            child { node {P1}}
            child { node {P2}}}
        child { node {M2}
            child { node {P1}}
            child { node {P2}}}}
    child { node {W2}
        child { node {M1}
            child { node {P1} }}}
    child { node {W3}
        child { node {M1} }
        child { node {M2} }};
    \end{tikzpicture}

   \begin{forest}
    for tree={% style of tree nodes
      draw, semithick, rounded corners,
        top color = red!10,
     bottom color = red!40,
               % style of tree (edges, distances, direction)
             grow = 270,
            s sep+ = 2mm,    % minimal (sibling) distance
            l sep+ = 4mm,    % level distance
               }
[Sentence
    [W1
        [M1
            [P1]
            [P2]
        ]
        [M2
            [P1]
            [P2]
        ]
    ]
    [W2
        [M1
            [P1]]
    ]
    [W3
        [M1]
        [M2]
    ]
];
    \end{forest}
\end{document}
    \end{document}

tikz

在此处输入图片描述

forest

在此处输入图片描述

森林的优点在于它负责照顾节点之间的距离。

相关内容