如何使树变宽

问题描述 投票:0回答:1

enter image description here

这是我通过以下代码制作的树:

\begin{tikzpicture}[sibling distance=7em,
  every node/.style = {shape=rectangle, rounded corners,
    draw,
    top color=white, bottom color=blue!20}]]
  \node {$ C_{i} $}
    child { node {$ q_{1}k_{1} $} 
    child{node{$q_{2}k_{2} $}}
     child{node{$\cdots$}}
         child{node{$q_{2}k_{2}+(k_{2}-1) $}}}
    child { node {$ \cdots$} }
    child { node {$ q_{1}k_{1} +(k_{1}-1)$} 
    child{node{$q_{2}k_{2} $}}
     child{node{$\cdots$}}
         child{node{$q_{2}k_{2}+(k_{2}-1) $}}}
;
\end{tikzpicture}

您可以看到两个分支都崩溃了,我想使树变宽以使其更好。

tree latex
1个回答
0
投票

如tikz手册中所述(第79页),“节点的重叠是由于TikZ在放置时并不是特别聪明子节点。“

有几种解决方法:

  • 在不同级别上使用不同的同级间距
  • 手移节点以避免重叠
  • 不要使用标准tikz命令将树工具用于大型节点和放置节点

1 /您可以根据节点的级别指定不同的图形参数。由于节点较大,因此在级别1中需要比在级别2中更大的间距。这可以通过在tikzpicture处添加来完成]

  level 2/.style={sibling distance=7em,},
  level 1/.style={sibling distance=10em,},

以及完整的代码


\begin{tikzpicture}[
  level 2/.style={sibling distance=7em,},
  level 1/.style={sibling distance=10em,},
  every node/.style = {shape=rectangle, rounded corners,
    draw,
    minimum height=6mm,
    top color=white, bottom color=blue!20}]]
  \node {$ C_{i} $}
    child { node {$ q_{1}k_{1} $} 
      child{node{$q_{2}k_{2} $}}
      child{node{$\cdots$}}
      child{node{$q_{2}k_{2}+(k_{2}-1) $}}}
    child { node {$ \cdots$} }
    child { node {$ q_{1}k_{1} +(k_{1}-1)$} 
      child{node{$q_{2}k_{2} $}}
      child{node{$\cdots$}}
      child{node{$q_{2}k_{2}+(k_{2}-1) $}}}
;
\end{tikzpicture}

enter image description here

2 //您可以通过xshift / yshift调整节点位置。因此,只需左移叶子3和右移叶子4即可避免重叠。

\begin{tikzpicture}[sibling distance=8em,
  every node/.style = {shape=rectangle, rounded corners,
    draw,
    minimum height=6mm,
    top color=white, bottom color=blue!20}]]
  \node {$ C_{i} $}
    child { node {$ q_{1}k_{1} $} 
      child{node{$q_{2}k_{2} $}}
      child{node{$\cdots$}}
      child{node[xshift=-2.5em]{$q_{2}k_{2}+(k_{2}-1) $}}}
    child { node {$ \cdots$} }
    child { node {$ q_{1}k_{1} +(k_{1}-1)$} 
      child{node[xshift=2.5em]{$q_{2}k_{2} $}}
      child{node{$\cdots$}}
      child{node{$q_{2}k_{2}+(k_{2}-1) $}}}
;
\end{tikzpicture}

enter image description here

3 /图形绘制工具对于绘制由相同节点(最好是简单的圆形或正方形)组成的图形非常有用。但是对于模式复杂的不规则节点,使用标准tikz定位命令更容易获得良好的结果。

这里是这样做的意思。首先将叶子均匀放置。然后是每个子树的中间节点之上的每个节点的中间层。最后,根部居中。我们需要定位库和计算库。

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}

\begin{document}
\begin{tikzpicture}[
  node distance=1em,
  every node/.style = {shape=rectangle, rounded corners,
    draw,
    minimum height=6mm,
    top color=white, bottom color=blue!20}]]
  % lay out the leaves
  \path node (n00) {$q_{2}k_{2} $}   node[right=of n00](n01){$\cdots$} node[right=of n01](n02){$q_{2}k_{2}+(k_{2}-1) $}
        node[right=2em of n02](n20){$q_{2}k_{2} $} node[right=of n20](n21){$\cdots$} node[right=of n21](n22){$q_{2}k_{2}+(k_{2}-1) $}
        ;

   % intermediate level with nodes aligned to the center child and the middle node centered
   \path node[above=1cm of n01] (n0) {$ q_{1}k_{1} $}
         node[above=1cm of n21] (n2) {$ q_{1}k_{1} +(k_{1}-1)$} 
         node[] (n1) at ($(n0.east)!0.5!(n2.west)$) {$ \cdots$} ;
    % the root above the center node
    \node[above=1cm of n1] (n) {$ C_{i} $};
    %% then draw the      vertices
    \foreach \i in {0,1,2} {
      \draw (n) -- (n\i) 
            (n0) -- (n0\i) 
            (n2) -- (n2\i) ;
     }
\end{tikzpicture}
\end{document}

enter image description here

由您决定要选择哪种解决方案。

无关,但我为每个节点都增加了最小高度,以使\ cdots节点的高度相同。

© www.soinside.com 2019 - 2024. All rights reserved.