由于 Tikz 树中的条目过多而导致重叠

由于 Tikz 树中的条目过多而导致重叠

我想使用Tikz 树为了表示分类,我的问题是我有很多条目,所以它们重叠,如果我按照建议增加间距这里图形将超出边界,

你能告诉我如何解决这个问题吗?提前谢谢。

在此处输入图片描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

\begin{figure}[ht!]
\begin{tikzpicture}
[   
    level 1/.style = {red,    sibling distance = 110mm, level distance = 8mm},
    level 2/.style = {blue,   sibling distance = 35mm,  level distance = 10mm},
    level 3/.style = {teal,   sibling distance = 30mm,  level distance = 12mm},
    level 4/.style = {violet, sibling distance = 06mm,  level distance = 30mm},
    level 5/.style = {gray,   sibling distance = 08mm,  level distance = 30mm},
    every node/.append style = {draw, align=center},
    grow=right,
    edge from parent path={(\tikzparentnode\tikzparentanchor) |- (\tikzchildnode\tikzchildanchor)},
]
\node {\textbf{Human Variability}}
    %
    child { node {Cognitive}
        %%
        child {node {individual}
            %%-%
            child {node {intra-personal}
                child { node {Situation Awareness}}
                child { node {Attention}}
                child { node {Motivation}}
            }
            child {node {inter-personal}
                %%-%%
                child {node {Cognitive Load}
                    %%-%%-%
                    child {node {Comprehension}}
                    child {node {Mental Capacity}}
                }
                child { node {Experience}}
                child { node {Satisfaction}}
                child { node {Trust}}
                child { node {Curiosity}}
                child { node {Frustration}}
                child { node {Acceptability}}
                child { node {Usability}}
                child { node {Task Engagement}}
            }
        }
        child {node {Temporal}
            %%-%
            child {node {Volatile}
                %%-%%
                child { node {Curiosity}}
                child { node {Situation Awareness}}
                child { node {Attention}}
                child { node[anchor=north]{Trust}}
                child { node {Frustration}}
                child { node {Task Engagement}}
                child { node {Motivation}}
            }
            child {node {Consistent}
                %%-%
                child {node {Cognitive Load}
                    %%-%%
                    child {node {Comprehension}}
                    child {node {Mental Capacity}}
                }
                child { node {Experience}}
                child { node [anchor=north]{Satisfaction}}
                child { node {Acceptability}}
                child { node {Usability}}
            }
        }
    }
    child {node {Physical}
        %%
        child {node {individual}
            %%-%
            child {node {intra-personal}
                %%-%%
                child {node [anchor= north]{Motor Variability}
                    %%-%%-%
                    child {node {Motion Trajectory}}
                    child {node [anchor= north]{Posture}}
                    child {node {Torque and Force}}
                }
                child {node [anchor=north]{Muscular Fatigue}}
                child{node{Heart Rate Variability (HRV)}}
            }
            child {node {inter-personal}
                %%-%%
                child {node [anchor= north]{laterality (dominant hand)}}
            }
            }
        child {node {Temporal}
            %%-%
            child {node {Volatile}
                %%-%%
                child { node {Motor Variability}
                    %%-%%-%
                    child {node {Motion Trajectory}}
                    child {node [anchor=north]{Posture}}
                    child {node {Torque and Force}}
                }
                child{node{Heart Rate Variability (HRV)}}
            }
            child {node {Consistent}
                %%-%%
                child {node [anchor=north]{laterality (dominant hand)}}
                child{ node{Anthropometric Variability}
                    %%-%%-%
                    child {node {Body Dimensions}}
                    child {node [anchor=north]{Gender}}
                    child {node {Age}}
                }
            }
        }
    }
;
\end{tikzpicture}
\caption{Human Variability Classification}
\label{fig:HVC}
\end{figure}

\end{document}

答案1

这是一个使用 的解决方案forest,它的语法稍微简单一些,并且可以有效地分配节点而不会使它们重叠。该forked edges选项使用edges库。

有两个稍微复杂的部分:首先,为了使节点按级别对齐,使用tier/.pgfmath=level(),这是手册(版本 2.1.5)第 74 页隐藏的一个技巧。

parent anchor=center其次,在前几层将边缘绘制到节点中心( )允许负片l sep,这样各层就可以在维度上重叠x,但随后边缘将绘制在文本之上。为了解决这个问题,我重新定义了内置的draw tree edge/.code,将所有边缘放在背景层上。

在此处输入图片描述

以下是代码:

\documentclass{article}

\usepackage[edges]{forest}

\pgfdeclarelayer{background}
\pgfsetlayers{background, main}

\makeatletter
\forestset{draw tree edge/.code={\begin{pgfonlayer}{background}\forest@draw@edge\end{pgfonlayer}}}
\makeatother

\begin{document}

\begin{figure}[ht!]
\begin{forest}
for tree={grow=east, draw, forked edges,
    l sep=1pt, anchor=base, s sep=1pt, 
    tier/.pgfmath=level(),
    if level=1{red, edge=red}{if level=2{blue, edge=blue}{if level=3{teal, edge=teal}{if level=4{violet, edge=violet}{if level=5{gray, edge=gray}{}}}}},
    fill=white, font=\small
    }
[Human Variability, l sep=-1cm, parent anchor=center
    [Cognitive, l sep=-5mm, parent anchor=center
        [individual, l sep=-5mm, parent anchor=center
            [intra-personal
                [Situation Awareness]
                [Attention]
                [Motivation]
            ]
            [inter-personal
                [Cognitive Load
                    [Comprehension]
                    [Mental Capacity]
                ]
                [Experience]
                [Satisfaction]
                [Trust]
                [Curiosity]
                [Frustration]
                [Acceptability]
                [Usability]
                [Task Engagement]
            ]
        ]
        [Temporal, l sep=-5mm, parent anchor=center
            [Volatile
                [Curiosity]
                [Situation Awareness]
                [Attention]
                [Trust]
                [Frustration]
                [Task Engagement]
                [Motivation]
            ]
            [Consistent
                [Cognitive Load
                    [Comprehension]
                    [Mental Capacity]
                ]
                [Experience]
                [Satisfaction]
                [Acceptability]
                [Usability]
            ]
        ]
    ]
    [Physical, l sep=-5mm, parent anchor=center
        [individual, l sep=-5mm, parent anchor=center
            [intra-personal
                [Motor Variability
                    [Motion Trajectory]
                    [Posture]
                    [Torque and Force]
                ]
                [Muscular Fatigue]
                [Heart Rate Variability (HRV)]
            ]
            [inter-personal
                [laterality (dominant hand)]
            ]
        ]
        [Temporal, l sep=-5mm, parent anchor=center
            [Volatile
                [Motor Variability
                    [Motion Trajectory]
                    [Posture]
                    [Torque and Force]
                ]
                [Heart Rate Variability (HRV)]
            ]
            [Consistent
                [laterality (dominant hand)]
                [Anthropometric Variability
                    [Body Dimensions]
                    [Gender]
                    [Age]
                ]
            ]
        ]
    ]
]
\end{forest}
\caption{Human Variability Classification}
\label{fig:HVC}
\end{figure}

\end{document}

相关内容