Tikz 树:节点大小和自动对齐

Tikz 树:节点大小和自动对齐

我正在使用 tikz 制作一棵树。有没有办法全局定义节点大小?如果我删除树节点中的文本,它们会变得非常小。我想为蓝色、橙色和红色节点定义不同的大小。有没有办法自动对齐节点?

\documentclass{article}
\usepackage[a4paper,margin=1cm,landscape]{geometry}
\usepackage{tikz}

%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>

\usetikzlibrary{positioning,shadows,arrows}

\begin{document}


\begin{center}
\begin{tikzpicture}[
    fact/.style={circle, draw=none, rounded corners=1mm, fill=gray, drop shadow,
        text centered, anchor=north, text=white},
    state/.style={circle, draw=none, fill=orange, circular drop shadow,
        text centered, anchor=north, text=white},
    empty/.style={circle, draw=none, fill=blue, circular drop shadow,
        text centered, anchor=north, text=white},
    leaf/.style={circle, draw=none, fill=red, circular drop shadow,
        text centered, anchor=north, text=white},
    level distance=0.5cm, growth parent anchor=south
]
\node (Fact00) [fact] {$root$}
[sibling distance=5cm]
child{ [sibling distance=1.2cm]
node (State00) [state] {$pp$}
    child{
    node (State09) [empty] {$$}
    }
    child{
    node (State09) [leaf] {$leaf$}
    }
    child{
    node (State09) [empty] {$$}
    }
    child{ [sibling distance=3cm]
    node (State09) [state] {$pp$}
        child{ [sibling distance=1cm]
        node (State10) [state] {$pp$}
            child{
            node (Leaf1) [leaf] {$leaf$}
            }
            child{
            node (Leaf1) [leaf] {$leaf$}
            }
        }
        child{ [sibling distance=1cm]
        node (State10) [state] {$pp$}
            child{
            node (Leaf1) [leaf] {$leaf$}
            }
            child{
            node (Leaf1) [leaf] {$leaf$}
            }
        }
    }
}
child{ [sibling distance=1.2cm]
node (State00) [state] {$pp$}
    child{
    node (State09) [leaf] {$leaf$}
    }
    child{
    node (State09) [leaf] {$leaf$}
    }
}
child{ [sibling distance=1.2cm]
node (State00) [state] {$pp$}
    child{
    node (State09) [leaf] {$leaf$}
    }
    child{
    node (State09) [leaf] {$leaf$}
    }
    child{
    node (State09) [leaf] {$leaf$}
    }
}
child{ [sibling distance=1.2cm]
    node (State01) [state] {$pp$}
    child{
    node (State11) [leaf] {$leaf$}
    }
    child{
    node (State09) [leaf] {$leaf$}
    }
}
;  
\end{tikzpicture}
\end{center}


\end{document}

在此处输入图片描述

答案1

您可以添加minimum size样式定义以强制执行最小尺寸。您无法使用此方法自动布局,但您可以使用诸如level <n>以下键的样式:sibling distance用于树/级别范围的控制)。

对于自动布局,请尝试 Forest 或算法图形绘制,它们是 Ti 的一部分Z 本身。后者需要 LuaTeX,并且不是特定于树的,但包含一个基于树的布局算法库。

\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}

\begin{document}
\begin{tikzpicture}[
  fact/.style={circle, draw=none, rounded corners=1mm, fill=gray, drop shadow, text centered, anchor=north, text=white},
  state/.style={circle, draw=none, fill=orange, circular drop shadow, text centered, anchor=north, text=white, minimum size=7mm},
  empty/.style={circle, draw=none, fill=blue, circular drop shadow, text centered, anchor=north, text=white, minimum size=9mm},
  leaf/.style={circle, draw=none, fill=red, circular drop shadow, text centered, anchor=north, text=white, minimum size=5mm},
  level distance=0.5cm,
  growth parent anchor=south,
  ]
    \node (Fact00) [fact] {$root$}
    [sibling distance=5cm]
    child{ [sibling distance=1.2cm]
      node (State00) [state] {$pp$}
      child{
        node (State09) [empty] {$$}
      }
      child{
        node (State09) [leaf] {$leaf$}
      }
      child{
        node (State09) [empty] {$$}
      }
      child{ [sibling distance=3cm]
        node (State09) [state] {$pp$}
        child{ [sibling distance=1cm]
          node (State10) [state] {$pp$}
          child{
            node (Leaf1) [leaf] {$leaf$}
          }
          child{
            node (Leaf1) [leaf] {$leaf$}
          }
        }
        child{ [sibling distance=1cm]
          node (State10) [state] {$pp$}
          child{
            node (Leaf1) [leaf] {$leaf$}
          }
          child{
            node (Leaf1) [leaf] {$leaf$}
          }
        }
      }
    }
    child{ [sibling distance=1.2cm]
      node (State00) [state] {$pp$}
      child{
        node (State09) [leaf] {$leaf$}
      }
      child{
        node (State09) [leaf] {$leaf$}
      }
    }
    child{ [sibling distance=1.2cm]
      node (State00) [state] {$pp$}
      child{
        node (State09) [leaf] {$leaf$}
      }
      child{
        node (State09) [leaf] {$leaf$}
      }
      child{
        node (State09) [leaf] {$leaf$}
      }
    }
    child{ [sibling distance=1.2cm]
      node (State01) [state] {$pp$}
      child{
        node (State11) [leaf] {$leaf$}
      }
      child{
        node (State09) [leaf] {$leaf$}
      }
    }
    ;
\end{tikzpicture}
\end{document}

强制最小尺寸

Forest 尽力自动放置节点,并且通常能够成功避免大多数树的重叠。它还被设计为尽可能紧凑地构建树(尽管如果需要更宽敞的树,可以改变这一点)。

以下是一个例子:

\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
\begin{document}
\begin{forest}
  colour me/.style={
    fill=#1,
    draw=#1,
  },
  for tree={
    circle,
    circular drop shadow,
    text=white,
    math content,
    edge+={draw=gray, thick},
  },
  where n children=0{% for the leaves
    colour me=red,
    minimum size=5mm,
  }{
    if level=0{% for the root
      colour me=gray,
      minimum size=6mm,
    }{
      colour me=orange,
      minimum size=2mm,
    },
  },
  delay={
    where content={}{% just the empty nodes
      colour me=blue,
      minimum size=12mm,
    }{},
  },
  [root
    [pp
      [][leaf][]
      [pp
        [pp
          [leaf][leaf]
        ]
        [pp
          [leaf][leaf]
        ]
      ]
    ]
    [pp
      [leaf][leaf]
    ]
    [pp
      [leaf][leaf][leaf]
    ]
    [pp
      [leaf][leaf]
    ]
  ]
\end{forest}
\end{document}

森林树

请注意,通过展开树的相关部分来腾出空间,可以自动容纳较大的蓝色节点。

此处的颜色采用基本样式,colour me根据树中的位置和节点内容按照以下规则进行配置:

  • 根节点?-> gray+ 尺寸规格;
  • 叶节点?-> red+ 尺寸规格;
  • 空节点?-> blue+ 尺寸规格;
  • 否则-> orange+ 尺寸规范。

通过将样式应用于各个节点可以实现更细粒度的控制,但是这种粗粒度的分类对于问题中的树来说已经足够了。

答案2

我建议进行以下更改:

  • 用于minimum size设置节点的最小尺寸,例如minimum size=7mm

  • 用于inner sep减少节点中的空白空间,例如inner sep=1pt

  • 提取所有节点共有的样式元素,例如

    common/.style={circle, draw=none, text centered, anchor=north,
      minimum size=7mm, inner sep=1pt, text=white, circular drop shadow}
    

    并按 的方式使用它state/.style={common, fill=orange}

  • 不要使用数学模式来排版斜体文本,\textit而是使用;例如\textit{leaf}而不是$leaf$

  • 使用命令来一致地格式化节点内容;我将其称为\nc下面。例如,定义

    \newcommand\nc[1]{\textit{#1}}
    

    并使用\nc{leaf}

在此处输入图片描述

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}
\begin{document}
\newcommand\nc[1]{\textit{#1}}
\begin{tikzpicture}[
    common/.style={circle, draw=none, text centered, anchor=north,
      minimum size=7mm, inner sep=1pt, text=white, circular drop shadow},
    fact/.style={common, fill=gray},
    state/.style={common, fill=orange},
    empty/.style={common, fill=blue},
    leaf/.style={common, fill=red},
    level distance=0.5cm, growth parent anchor=south
]
\node (Fact00) [fact] {\nc{root}}
[sibling distance=5cm]
child{ [sibling distance=1.2cm]
node (State00) [state] {\nc{pp}}
    child{
    node (State09) [empty] {}
    }
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
    child{
    node (State09) [empty] {}
    }
    child{ [sibling distance=3cm]
    node (State09) [state] {\nc{pp}}
        child{ [sibling distance=1cm]
        node (State10) [state] {\nc{pp}}
            child{
            node (Leaf1) [leaf] {\nc{leaf}}
            }
            child{
            node (Leaf1) [leaf] {\nc{leaf}}
            }
        }
        child{ [sibling distance=1cm]
        node (State10) [state] {\nc{pp}}
            child{
            node (Leaf1) [leaf] {\nc{leaf}}
            }
            child{
            node (Leaf1) [leaf] {\nc{leaf}}
            }
        }
    }
}
child{ [sibling distance=1.2cm]
node (State00) [state] {\nc{pp}}
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
}
child{ [sibling distance=1.2cm]
node (State00) [state] {\nc{pp}}
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
}
child{ [sibling distance=1.2cm]
    node (State01) [state] {\nc{pp}}
    child{
    node (State11) [leaf] {\nc{leaf}}
    }
    child{
    node (State09) [leaf] {\nc{leaf}}
    }
}
;  
\end{tikzpicture}
\end{document}

相关内容