TiKz樹狀圖 - paraquat的部落格 - udn部落格
paraquat的部落格
作家:paraquat
文章分類
    Top
    TiKz樹狀圖
    2015/04/19 17:06:43
    瀏覽:2791
    迴響:0
    推薦:0
    引用0
    TiKz本身就能很輕鬆的畫出樹狀圖。有一個child命令,可以成為node的「孩子」,可以一對一或一對多:

    \node{tree}

    child{node{left}}

    child{node{middle}}

    child{node{right}

    child {node {child1}}

    child {node{child 2}}

    };

    這是基本的樹狀圖。一個樹狀圖裡面,有方向,親子距離,子子距離,「代」數,連線等等的關係,一些參數,可以對樹狀圖做控制:

    grow, 樹狀圖的發展方向。如上例中,加入參數:

    \node{tree}[grow=up]

    ...

    \child{node{right}[grow=left]

    就變成往上生長的樹狀圖, right的子代則往左生長。也可以使用某一個角度值,也可以各別控制不同child的方向。

    level distance 是親子之間的長度。

    sibling distance 是子子之間的間距。

    every child, every child node, level等參數都是style形態,能對child, child node, 不同代數之間做出不同的設定。如下例:

    \begin{tikzpicture}

    [level distance=30mm,

    every node/.style={fill=red!60,inner sep=1pt]},

    level 1/.style={nodes={fill=red!45},sibling distance=40mm},

    level 2/.style={nodes={fill=red!30},sibling distance=20mm},

    level 3/.style={nodes={fill=red!15},sibling distance=10mm}]

    \node{祖先}[grow=right]

    child{node{第一代A}

    child{node{第二代AA}

    child{node{第三代AAA}}

    child{node{第三代AAB}}}

    child{node{第二代AB}

    child{node{第三代ABA}}

    child{node{第三代ABB}}}

    child{node{第一代B}

    child{node{第二代BA}

    child{node{第三代BAA}}

    child{node{第三代BAB}}}

    child{node{第二代BB}

    child{node{第三代BBA}}

    child{node{第三代BBB}}}

    };

    \end{tikzpicture}

    edge from parent 連上一代的線條,其上也可以加上標示。如

    \node{root}

    child{node{A} edge from parrent node[right,textcolor=red]{a}}如下例a

    child{node{B}edge form parrent node[fill=white,textcolor=red, inner sep=1pt]{b}}下例b

    edge from parent path連到上一代的線條的參數控制,其預設值是(\tikzparentnode\tikzparentanchor) -- (\tikzchildnode\tikzchildanchor),可以設定成曲線。

    child{node{C}[red,edge from parent path={ (\tikzparentnode.south)to[out=270,in=90]node[fill=white,inner sep=1pt,text=red]{c}(\tikzchildnode.north)]}}如下例c

    child{node{D}edge form parent[draw=none] node[text=red]{d}} 不畫出連線如下例d。

    回應
    全站分類:知識學習 科學百科
    自訂分類:LaTex 排版環境
    上一則: TiKz 的foreach廻圈
    下一則: TiKZ 的常用路徑(path)

    限會員,要發表迴響,請先登入