PGF 自动确定树节点/级别之间的垂直距离

PGF 自动确定树节点/级别之间的垂直距离

我正在尝试绘制树状概览,并从中获取了一个例子http://www.texample.net。但我对节点(级别)之间的垂直距离有疑问。有没有办法让 tikz 自动确定距离,这样就不会重叠。例如,对于大型树,对每个节点使用“level distance=x.yem”是一项繁重的工作。下面是树的一小部分的运行示例。

            \documentclass[paper=a4]{report}
            \usepackage[utf8]{inputenc} % UTF8 encoding

            \usepackage{pgfplots}
            \pgfplotsset{compat=newest}
            \pgfplotsset{plot coordinates/math parser=false}
            \usetikzlibrary{arrows,shapes,positioning,shadows,trees}

            \begin{document}



            \tikzset{
                basic/.style  = {draw, font=\sffamily, rectangle},
                root/.style   = {basic, rounded corners=6pt, thin, align=center,
                                                 fill=blue!60,text width=0.5\textwidth},
                level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=green!60,text width=0.3\textwidth},
                level 3/.style = {basic, rounded corners=2pt, thin, align=center, fill=pink!60,text width=0.2\textwidth},
            }

            \begin{tikzpicture}[level 1/.style={sibling distance=40mm},
                edge from parent/.style={->,draw},
                >=latex]

            % root of the the initial tree, level 1
            \node[root]
            [level distance=30mm]
            {Some loss calculation models}
            % The first level, as children of the initial tree
                child {node[level 2] (c1) {ALmann based models (analytical)}}
                child {node[level 2] (c2) {Loss separation stuff stuff}}
                child {node[level 2] (c3) {Mathematical models and others}};

            % The second level, relatively positioned nodes
            \begin{scope}[every node/.style={level 3}]
            \node [below of = c1, xshift=15pt] (c11) {Mathematic stuff with very long name};
            \node [below of = c11] (c12) {Mathematic2 other long name};
            \node [below of = c12] (c13){Mathematic3 short};
            \node [below of = c13] (c13){Mathematic4 meadium size name};

            \end{scope}

            % lines from each level 1 node to every one of its "children"
            \foreach \value in {1,2,3}
                \draw[->] (c1.195) |- (c1\value.west);

            \end{tikzpicture}
            \end{document}

答案1

如果你不想等待TiKZ 3它的graphdrawing库,一个有效的选择可能是forest包。它根据节点的大小自动计算节点之间的距离。

下一个代码显示了与您的树或多或少相似的树的起点。您需要阅读其手册才能了解如何调整其父级下的垂直子树。您还可以在 TX.SX 中查看有关有trees和无的先前问题以获得帮助forest

\documentclass[tikz,border=3mm]{standalone}
\usepackage[utf8]{inputenc} % UTF8 encoding

\usepackage{forest}

\begin{document}
    \tikzset{basic/.style = {draw, font=\sffamily, rectangle},
            root/.style = {basic, rounded corners=6pt, thin,
                 align=center, fill=blue!60, 
                 text width=0.5\textwidth},
            level 2/.style = {basic, rounded corners=6pt, thin, 
                   align=center, fill=green!60,
                   text width=0.3\textwidth},
            level 3/.style = {basic, rounded corners=2pt, thin,
                   align=center, fill=pink!60,
                   text width=0.2\textwidth},
            }

\begin{forest}
% root of the the initial tree, level 1
[Some loss calculation models, root, for tree={level 2}
% The first level, as children of the initial tree
     [Almann based models (analytical), 
            for descendants={level 3, child anchor=west}, 
            for tree={ grow'=east }, calign=first
% The second level
        [Mathematic stuff with very long name]
        [Mathematic2 other long name]
        [Mathematic3 short]
        [Mathematic4 meadium size name]]
     [Loss separation stuff stuff]
     [Mathematical models and others]]
\end{forest}

\end{document}

在此处输入图片描述

相关内容