如何設定 tikz-qtree 樹​​中各個兄弟姊妹之間的距離?

如何設定 tikz-qtree 樹​​中各個兄弟姊妹之間的距離?

我為我的研究計畫製作了這棵樹。我剛剛重命名了這個問題的所有屬性。從下圖可以看到,上面的兄弟姊妹非常擁擠,整個畫面看起來非常糟糕。如何在特定級別的各個兄弟姐妹之間建立距離?有沒有其他方法可以美化這張圖,使其更像專業的圖?

代碼:

\documentclass[12pt]{article}

\usepackage[a4paper]{geometry}% just to demonstrate the effects

\usepackage{graphicx}
\usepackage{qtree}

%\usetikzlibrary{arrows}
\usepackage{tikz-qtree}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

%\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{arrows}


\begin{document}

\resizebox{1\linewidth}{!}{%
\begin{tikzpicture}[grow=right]
\tikzset{every tree node/.style={align=center}}
\tikzset{level distance=90pt}
\tikzset{level 1/.style={sibling distance=-50pt}}
\tikzset{edge from parent/.append style={thick,-stealth}}
%\tikzset{execute at begin node=\strut}
\Tree[.text1length1 
        [.textlength1\\text2 
            [.text1lenght2 ]
            [.asdsdsd 
                [.asdasdsd 
                    [.text1leng
                        [.text1lenght2 ]
                        [.asdasdsd [.text1leng [.text1lengAcid ] ] ]
                    ]
                    [.text1leng [.text1lengAcid ]]
                ] %asdasdsd
                [.asdEERER 
                    [.text1lenght2 ]
                    [.asdasdsd [.text1leng [.text1lengAcid ] ] ]
                ]
                [.text1lenght2 ]
            ] %asdsdsd
            [.aASDASD 
                [.text1leng [.text1lengAcid ]
                [.ASDERESSD 
                    [.text1lenght2 ]]
                    [.asdsdsd
                        [.text1lenght2 ]
                        [.asdEERER 
                            [.text1lenght2 ] 
                            [.asdasdsd [.text1leng [.text1lengAcid ] ] ]
                        ]
                        [.asdasdsd
                            [.text1leng ][.text1lenght2 ]
                            [.text1leng [.text1lengAcid ] ]
                        ]
                    ]
                    [.aASDASD
                        [.text1leng [.text1lengAcid ] ]
                        [.ASDERESSD 
                            [.text1lenght2 ] 
                            [.asdsdsd 
                                [.text1lenght2 ] 
                                [.asdEERER [.text1lenght2 ] ]
                            ]
                        ]
                    ]
                ] 
            ] 
        ] 
        [.HelloWorld111     
            [.HelloWorld333 ] 
            [.text1leng [.text1lengAcid ] ]
        ]
        [.backPress     
            [.text1lengAcid ] 
        ]
        [.Hello ]
        [.World11333    
            [.HelloWorld333 ] 
            [.Jango ]
            [.text1leng [.text1lengAcid ] ]
        ]
    ]
\end{tikzpicture}
}

\end{document}

輸出:

在此輸入影像描述

答案1

這是一個變體你使用的樹forest。請注意,這裡的部分問題是我不知道如何用連字號連接類似text1lenght2.因此我認為這些不能用連字號連接。如果可以,您可以創造更好的結果。

為了避免讓樹超出合理範圍擴展,我定義了一種樣式stepwise={}{},它將逐步採用父級和子級之間的路徑。標準路徑以水平/垂直方式移動,而不是像老鷹一樣直線飛行,因為這減少了邊緣與節點碰撞的機會。

請注意,如果您使用向南生長的樹,則不需要執行這些操作,因為不會發生碰撞。

生成的樹相對緊湊:它將適合 A4 風景,無需縮放。您可能知道,如果可能的話,應避免使用比例圖。

結果如下:

逐步樹

這是代碼:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{forest}
\begin{document}
\tikzset{>=latex}

\forestset{
  stepwise/.style n args=2{
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- +(#1,#2) |- (.child anchor)\forestoption{edge label};
    }
  }
}

\begin{forest}
  for tree={
    edge=->,
    grow=east,
    align=left,
    child anchor=west,
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
    },
    font=\sffamily
  }
  [text1length1
    [textlength1\\text2
        [text1lenght2
        ]
        [asdsdsd
            [asdasdsd, stepwise={30pt}{-20pt}
                [text1leng
                    [text1lenght2]
                    [asdasdsd [text1leng [text1lengAcid]]]
                ]
                [text1leng [text1lengAcid]]
            ] %asdasdsd
            [asdEERER
                [text1lenght2]
                [asdasdsd [text1leng [text1lengAcid]]]
            ]
            [text1lenght2]
        ] %asdsdsd
        [aASDASD
            [text1leng [text1lengAcid]
            [ASDERESSD
                [text1lenght2]]
                [asdsdsd
                    [text1lenght2]
                    [asdEERER
                        [text1lenght2]
                        [asdasdsd [text1leng [text1lengAcid]]]
                    ]
                    [asdasdsd
                        [text1leng ][text1lenght2]
                        [text1leng [text1lengAcid]]
                    ]
                ]
                [aASDASD
                    [text1leng [text1lengAcid]]
                    [ASDERESSD
                        [text1lenght2]
                        [asdsdsd
                            [text1lenght2]
                            [asdEERER [text1lenght2]]
                        ]
                    ]
                ]
            ]
        ]
    ]
    [HelloWorld111
        [HelloWorld333]
        [text1leng [text1lengAcid]]
    ]
    [backPress
        [text1lengAcid]
    ]
    [Hello]
    [World11333
        [HelloWorld333, stepwise={25pt}{-15pt}]
        [Jango]
        [text1leng [text1lengAcid]]
    ]
  ]
\end{forest}

\end{document}

編輯沒有文字可能會更好,這也使樹在水平方向上更加緊湊。

不建議在所有情況下使用,但動態著色樹也是一種可能:

彩色樹

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{forest}
\begin{document}
\tikzset{>=latex}

\forestset{
  stepwise/.style n args=2{
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- +(#1,#2) |- (.child anchor)\forestoption{edge label};
    }
  },
  my shading/.style={
    for tree={
      text/.wrap pgfmath arg={black!##1!#1}{10*level()},
      edge/.wrap pgfmath arg={->, draw=black!##1!#1}{10*level()},
    },
  },
}

\begin{forest}
  for tree={
    edge=->,
    grow=east,
    align=left,
    child anchor=west,
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
    },
    font=\sffamily
  }
  [text1length1
    [textlength1\\text2, my shading=blue
        [text1lenght2, my shading=green
        ]
        [asdsdsd, my shading=blue!50!green
            [asdasdsd, stepwise={30pt}{-20pt}
                [text1leng
                    [text1lenght2]
                    [asdasdsd [text1leng [text1lengAcid]]]
                ]
                [text1leng [text1lengAcid]]
            ] %asdasdsd
            [asdEERER
                [text1lenght2]
                [asdasdsd [text1leng [text1lengAcid]]]
            ]
            [text1lenght2]
        ] %asdsdsd
        [aASDASD
            [text1leng [text1lengAcid]
            [ASDERESSD
                [text1lenght2]]
                [asdsdsd
                    [text1lenght2]
                    [asdEERER
                        [text1lenght2]
                        [asdasdsd [text1leng [text1lengAcid]]]
                    ]
                    [asdasdsd
                        [text1leng ][text1lenght2]
                        [text1leng [text1lengAcid]]
                    ]
                ]
                [aASDASD
                    [text1leng [text1lengAcid]]
                    [ASDERESSD
                        [text1lenght2]
                        [asdsdsd
                            [text1lenght2]
                            [asdEERER [text1lenght2]]
                        ]
                    ]
                ]
            ]
        ]
    ]
    [HelloWorld111, my shading=purple
        [HelloWorld333]
        [text1leng [text1lengAcid]]
    ]
    [backPress, my shading=red
        [text1lengAcid]
    ]
    [Hello, my shading=magenta]
    [World11333, my shading=orange
        [HelloWorld333, stepwise={25pt}{-15pt}]
        [Jango]
        [text1leng [text1lengAcid]]
    ]
  ]
\end{forest}

\end{document}

答案2

為了讓畫面更好看,我終於弄清楚如何安裝forest了。我從 StatMenu->Programs->MikTex->PackageManger 開啟 MikTex 的套件管理器。我安裝了森林。從安裝目錄中,我複製了forest.sty並貼上到目前工作目錄。使用森林,這就是上圖的樣子。

代碼:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{forest}

\begin{document}
\tikzset{>=latex}


\resizebox{0.8\textwidth}{!}{%
\begin{forest} for tree={align=center,edge=->,grow=east}
 [text1length1 
        [textlength1\\text2 
            [text1lenght2 ]
            [asdsdsd 
                [asdasdsd 
                    [text1leng
                        [text1lenght2 ]
                        [asdasdsd [text1leng [text1lengAcid ] ] ]
                    ]
                    [text1leng [text1lengAcid ]]
                ] %asdasdsd
                [asdEERER 
                    [text1lenght2 ]
                    [asdasdsd [text1leng [text1lengAcid ] ] ]
                ]
                [text1lenght2 ]
            ] %asdsdsd
            [aASDASD 
                [text1leng [text1lengAcid ]
                [ASDERESSD 
                    [text1lenght2 ]]
                    [asdsdsd
                        [text1lenght2 ]
                        [asdEERER 
                            [text1lenght2 ] 
                            [asdasdsd [text1leng [text1lengAcid ] ] ]
                        ]
                        [asdasdsd
                            [text1leng ][text1lenght2 ]
                            [text1leng [text1lengAcid ] ]
                        ]
                    ]
                    [aASDASD
                        [text1leng [text1lengAcid ] ]
                        [ASDERESSD 
                            [text1lenght2 ] 
                            [asdsdsd 
                                [text1lenght2 ] 
                                [asdEERER [text1lenght2 ] ]
                            ]
                        ]
                    ]
                ] 
            ] 
        ] 
        [HelloWorld111     
            [HelloWorld333 ] 
            [text1leng [text1lengAcid ] ]
        ]
        [backPress     
            [text1lengAcid ] 
        ]
        [Hello ]
        [World11333    
            [HelloWorld333 ] 
            [Jango ]
            [text1leng [text1lengAcid ] ]
        ]
    ]
\end{forest}
}
\end{document}

圖片:

在此輸入影像描述

相關內容