如何将 Tikz 树中的兄弟节点放置在与根节点不同长度的位置?

如何将 Tikz 树中的兄弟节点放置在与根节点不同长度的位置?

我如何才能改变同一层级上节点的边长?我的树太宽了,一页都放不下。如果我交替改变深度 1 上的节点深度,我想树可能可以放在一页上。

EnergyMotivation、HealthMotivation、SleepQualityMotivation 处于正常距离,而 ShapeMotivation 和 AttractionnessMotivation 则距离根稍远。

\documentclass[11pt,a4paper,oneside,notitlepage]{book}

\usepackage{graphicx}              
\usepackage{forest}
\usepackage{rotating} 

\begin{document}
\begin{sidewaysfigure}
\centering
    \begin{forest}
    for tree={
        rectangle,
        black,
        draw,
        minimum size = 0em,
        edge={<-},
        l sep = 45pt,
        s sep = 1pt,
    },
    arrow label/.style={
        edge label={node [midway, font=\scriptsize\sffamily, sloped, above] {#1}}
    }
    [\textbf{MotivationProfile}, fill=green!20
        [EnergyMotivation, arrow label=isA, fill=green!20
            [Low, arrow label=isA, fill=green!20
                [1, rounded rectangle, arrow label=isA, fill=blue!20]
                [2, rounded rectangle, arrow label=isA, fill=blue!20]
                [3, rounded rectangle, arrow label=isA, fill=blue!20]
                [4, rounded rectangle, arrow label=isA, fill=blue!20]

            ]
            [Medium, arrow label=isA, fill=green!20
                [5, rounded rectangle, arrow label=isA, fill=blue!20]
                [6, rounded rectangle, arrow label=isA, fill=blue!20]
            ]
            [High, arrow label=isA, fill=green!20
                [7, rounded rectangle, arrow label=isA, fill=blue!20]
                [8, rounded rectangle, arrow label=isA, fill=blue!20]
                [9, rounded rectangle, arrow label=isA, fill=blue!20]
                [10, rounded rectangle, arrow label=isA, fill=blue!20]
            ]
        ]
        [ShapeMotivation, arrow label=isA, fill=green!20
            [Low, arrow label=isA, fill=green!20
                [1, rounded rectangle, arrow label=isA, fill=blue!20]
                [2, rounded rectangle, arrow label=isA, fill=blue!20]
                [3, rounded rectangle, arrow label=isA, fill=blue!20]
                [4, rounded rectangle, arrow label=isA, fill=blue!20]

            ]
            [Medium, arrow label=isA, fill=green!20
                [5, rounded rectangle, arrow label=isA, fill=blue!20]
                [6, rounded rectangle, arrow label=isA, fill=blue!20]
            ]
            [High, arrow label=isA, fill=green!20
                [7, rounded rectangle, arrow label=isA, fill=blue!20]
                [8, rounded rectangle, arrow label=isA, fill=blue!20]
                [9, rounded rectangle, arrow label=isA, fill=blue!20]
                [10, rounded rectangle, arrow label=isA, fill=blue!20]
            ]
        ]
        [HealthMotivation, arrow label=isA, fill=green!20
            [Low, arrow label=isA, fill=green!20
                [1, rounded rectangle, arrow label=isA, fill=blue!20]
                [2, rounded rectangle, arrow label=isA, fill=blue!20]
                [3, rounded rectangle, arrow label=isA, fill=blue!20]
                [4, rounded rectangle, arrow label=isA, fill=blue!20]

            ]
            [Medium, arrow label=isA, fill=green!20
                [5, rounded rectangle, arrow label=isA, fill=blue!20]
                [6, rounded rectangle, arrow label=isA, fill=blue!20]
            ]
            [High, arrow label=isA, fill=green!20
                [7, rounded rectangle, arrow label=isA, fill=blue!20]
                [8, rounded rectangle, arrow label=isA, fill=blue!20]
                [9, rounded rectangle, arrow label=isA, fill=blue!20]
                [10, rounded rectangle, arrow label=isA, fill=blue!20]
            ]
        ]
        [AttractivenessMotivation, arrow label=isA, fill=green!20
                [Low, arrow label=isA, fill=green!20
                    [1, rounded rectangle, arrow label=isA, fill=blue!20]
                    [2, rounded rectangle, arrow label=isA, fill=blue!20]
                    [3, rounded rectangle, arrow label=isA, fill=blue!20]
                    [4, rounded rectangle, arrow label=isA, fill=blue!20]

                ]
                [Medium, arrow label=isA, fill=green!20
                    [5, rounded rectangle, arrow label=isA, fill=blue!20]
                    [6, rounded rectangle, arrow label=isA, fill=blue!20]
                ]
                [High, arrow label=isA, fill=green!20
                    [7, rounded rectangle, arrow label=isA, fill=blue!20]
                    [8, rounded rectangle, arrow label=isA, fill=blue!20]
                    [9, rounded rectangle, arrow label=isA, fill=blue!20]
                    [10, rounded rectangle, arrow label=isA, fill=blue!20]
                ]
        ]
        [SleepQualityMotivation, arrow label=isA, fill=green!20
                [Low, arrow label=isA, fill=green!20
                    [1, rounded rectangle, arrow label=isA, fill=blue!20]
                    [2, rounded rectangle, arrow label=isA, fill=blue!20]
                    [3, rounded rectangle, arrow label=isA, fill=blue!20]
                    [4, rounded rectangle, arrow label=isA, fill=blue!20]

                ]
                [Medium, arrow label=isA, fill=green!20
                    [5, rounded rectangle, arrow label=isA, fill=blue!20]
                    [6, rounded rectangle, arrow label=isA, fill=blue!20]
                ]
                [High, arrow label=isA, fill=green!20
                    [7, rounded rectangle, arrow label=isA, fill=blue!20]
                    [8, rounded rectangle, arrow label=isA, fill=blue!20]
                    [9, rounded rectangle, arrow label=isA, fill=blue!20]
                    [10, rounded rectangle, arrow label=isA, fill=blue!20]
                ]
        ]
    ] 
\end{forest}
\caption{Close view on the motivation profile concept}
\label{image:ontology-motivation}
\end{sidewaysfigure}

答案1

enter image description here

  • 所有距离forest都是相等的,节点 7、8、9 和 10 的定位差异是Hight由于字母导致的节点高度不同g。这可以通过定义text depth“绿色”节点来消除。
  • 森林代码中不允许有空行
  • 下面的 mwe 对您的代码做了一些改进,使您的代码变得更短。因为在当时,当我编写代码时,您的文档前言尚不清楚,所以我使用standalone文档类。您可以用您的前言替换它,并添加缺失的sidewaysfigure标题(这部分代码对于提出的解决方案并不是必不可少的):

\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
    \begin{forest}
    for tree={
        rectangle,
        draw,
        if n children=0{rounded corners=2pt,fill=blue!20,
                        inner sep=2pt, anchor=north}%
                       {fill=green!20,
                        text depth=0.25ex}, % <--- solve your problem
        edge={{Straight Barb[length=2pt, width=2pt]}-},
        l sep = 45pt,
        s sep = 3pt,
    },
    arrow label/.style={
        edge label={node [pos=0.7, font=\scriptsize\sffamily,
                          inner sep=1pt, sloped, above] {#1}}
    }
    [\textbf{MotivationProfile},
        [EnergyMotivation, arrow label=isA
            [Low, arrow label=isA
                [1, arrow label=isA]
                [2, arrow label=isA]
                [3, arrow label=isA]
                [4, arrow label=isA]
            ]
            [Medium, arrow label=isA
                [5, arrow label=isA ]
                [6, arrow label=isA ]
            ]
            [High, arrow label=isA
                [7, arrow label=isA ]
                [8, arrow label=isA ]
                [9, arrow label=isA ]
                [10, arrow label=isA ]
            ]
        ]
        [ShapeMotivation, arrow label=isA
            [Low, arrow label=isA
                [1, arrow label=isA ]
                [2, arrow label=isA ]
                [3, arrow label=isA ]
                [4, arrow label=isA ]
            ]
            [Medium, arrow label=isA
                [5, arrow label=isA ]
                [6, arrow label=isA ]
            ]
            [High, arrow label=isA
                [7, arrow label=isA ]
                [8, arrow label=isA ]
                [9, arrow label=isA ]
                [10, arrow label=isA ]
            ]
        ]
        [HealthMotivation, arrow label=isA
            [Low, arrow label=isA
                [1, arrow label=isA ]
                [2, arrow label=isA ]
                [3, arrow label=isA ]
                [4, arrow label=isA ]
            ]
            [Medium, arrow label=isA
                [5, arrow label=isA ]
                [6, arrow label=isA ]
            ]
            [High, arrow label=isA
                [7, arrow label=isA ]
                [8, arrow label=isA ]
                [9, arrow label=isA ]
                [10, arrow label=isA ]
            ]
        ]
        [AttractivenessMotivation, arrow label=isA
                [Low, arrow label=isA
                    [1, arrow label=isA ]
                    [2, arrow label=isA ]
                    [3, arrow label=isA ]
                    [4, arrow label=isA ]
                ]
                [Medium, arrow label=isA
                    [5, arrow label=isA ]
                    [6, arrow label=isA ]
                ]
                [High, arrow label=isA
                    [7, arrow label=isA ]
                    [8, arrow label=isA ]
                    [9, arrow label=isA ]
                    [10, arrow label=isA ]
                ]
        ]
        [SleepQualityMotivation, arrow label=isA
                [Low, arrow label=isA
                    [1, arrow label=isA ]
                    [2, arrow label=isA ]
                    [3, arrow label=isA ]
                    [4, arrow label=isA ]
                ]
                [Medium, arrow label=isA
                    [5, arrow label=isA ]
                    [6, arrow label=isA ]
                ]
                [High, arrow label=isA
                    [7, arrow label=isA ]
                    [8, arrow label=isA ]
                    [9, arrow label=isA ]
                    [10, arrow label=isA ]
                ]
        ]
    ]
\end{forest}
\end{document}

相关内容