树形图间距 Tikz

树形图间距 Tikz

我无法修复树的间距。

目前,我有以下代码和结果:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees, arrows}
\begin{document}
\pagestyle{empty}

    % Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=4cm, sibling distance=4cm]
\tikzstyle{level 2}=[level distance=4cm, sibling distance=4cm]
\tikzstyle{level 3}=[level distance=20mm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em,text centered]

\begin{tikzpicture}[grow=right,->,>=angle 60, sloped]
%\begin{scope}[yshift=0]
  \node[bag, minimum width=2cm] {Testing Consumer $t=0$}
    child {
        node[bag, minimum width=2cm] {$E[u(c,l,\eta_1)|K]$}
         edge from parent         
            node[above] {$\kappa = 0$}
    }
    child {node[bag, minimum width=2cm] {t=1 $E[u(c',l',\eta_{k,1})|K]$ }
      child {node{$E[u(c',l',\eta_{k,1})|\eta_0]$}
            edge from parent         
            node[above] {$\kappa = 0$}
        }
      child {node[bag, minimum width=4cm] {$E[u(c',l',\eta_k)|K]$}
            edge from parent         
            node[above] {$\kappa = 1$}
        }
            edge from parent         
            node[above] {$\kappa = 1$}
    }
        
        ;
%\end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

但我希望有一个更整洁的图像,子节点的标签也有更多的间距。任何帮助都非常感谢 :)

答案1

也许像这样?我替换了已弃用的\tikzstyle

![在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees, arrows}
\begin{document}
\pagestyle{empty}

% tikzset instead of deprecated tikzstyle
\tikzset{
    level 1/.style={level distance=4cm, sibling distance=6cm},
    level 2/.style={level distance=4cm, sibling distance=3cm},
    level 3/.style={level distance=3cm, sibling distance=2cm},
    bag/.style={text width=8em, text centered}
}

\begin{tikzpicture}[grow=right,->,>=angle 60, sloped]
  \node[bag, minimum width=2cm] {Testing Consumer $t=0$}
    child {
        node[bag, minimum width=2cm] {$E[u(c,l,\eta_1)|K]$}
         edge from parent         
            node[above] {$\kappa = 0$}
    }
    child {node[bag, minimum width=2cm] {t=1 $E[u(c',l',\eta_{k,1})|K]$ }
      child {node{$E[u(c',l',\eta_{k,1})|\eta_0]$}
            edge from parent         
            node[above] {$\kappa = 0$}
        }
      child {node[bag, minimum width=2cm] {$E[u(c',l',\eta_k)|K]$}
            edge from parent         
            node[above] {$\kappa = 1$}
        }
            edge from parent         
            node[above] {$\kappa = 1$}
    };
\end{tikzpicture}
\end{document}


  [1]: 

https://i.stack.imgur.com/n0X1o.png

答案2

我会使用forest,但这istgame也是个不错的选择。可能是这样的?

\begin{forest}
  align=center,

这仅适用于根节点。子节点的对齐方式如下。

  for tree={%
    grow'=east,
    calign=midpoint,
    parent anchor=children,
    child anchor=parent,
    anchor=child anchor,

基本上,将父节点与子节点中间的点对齐,将子节点根据其 s 对齐在同一水平,child anchor并为树设置生长方向和锚点。这还可以确保分支边缘从一个公共点开始,这似乎是概率/游戏树的常见做法。

    l sep'+=25pt,

增加级别之间的距离,即每个父级与其子级之间的距离。这可能还不够。

    s sep'+=20pt,

增加兄弟姐妹之间的最小间隔。

    edge+={-Stealth},

在所有边上添加箭头。

    font=\sffamily,

文本使用 sans。这通常对于图表来说是一个好主意,但在这种情况下,考虑到数学与文本的比例很高,你可能想要也可能不想要它。

  },
  for descendants={%

以下内容影响所有后代,即除根之外的所有内容。

    if n=1{edge label={node[above,midway,sloped,font=\scriptsize]{$\kappa=1$}}}{
      edge label={node[below,midway,sloped,font=\scriptsize]{$\kappa=0$}},
    },

如果我们处理第一个孩子,则将边缘标签设置为$\kappa=1$并将其放在线上方;如果是第二个,则使用$\kappa=0并将其设置在下面。

    align=left,

这些节点的对齐。center如果愿意,请在此处使用。

  },

最后,是树本身。我手动折线,因为这样更容易得到一个整洁的结果,而且不需要做太多。

  [{Testing\\Consumer\\$t=0$}
    [{t=1\\$E[u(c',l',\eta_{k,1})|K]$}
      [{$E[u(c',l',\eta_k)|K]$}
      ]
      [{$E[u(c',l',\eta_{k,1})|\eta_0]$}
      ]
    ]
    [{$E[u(c,l,\eta_1)|K]$}
    ]
  ]
\end{forest}

请注意,由于 Okular/KDE 错误,我目前无法生成有价值的输出图像。所以我的输出在 PDF 中看起来不错,但当我将其发布到这里时却很糟糕。

树与森林

我不确定t=0等指定什么。如果这些适用于某一级别的所有节点,我会将这些标记放在树的上方或下方。例如,您可以添加类似

  before typesetting nodes={%
    tikz+={\coordinate (c) at ([yshift=5pt]current bounding box.north);},
    tempcounta/.max={level}{tree},
    for nodewalk={%
      do while={>OR<{level}{tempcounta}}{fake=next node},
      current and ancestors
    }{%
      tikz+/.process={Ow{level}{%
          \node [anchor=base] at ( |- c) {$t=#1$};
        }
      },
    },
  },

在序言中为顶部的级别创建标签。

顶部每个级别的标签

完整代码:

\documentclass[tikz]{standalone}
% ateb: https://tex.stackexchange.com/a/713428/ addaswyd o gwestiwn MarV: https://tex.stackexchange.com/q/713292/
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
  align=center,
  for tree={%
    grow'=east,
    calign=midpoint,
    parent anchor=children,
    child anchor=parent,
    anchor=child anchor,
    l sep'+=25pt,
    s sep'+=20pt,
    edge+={-Stealth},
    font=\sffamily,
  },
  for descendants={%
    if n=1{edge label={node[above,midway,sloped,font=\scriptsize]{$\kappa=1$}}}{
      edge label={node[below,midway,sloped,font=\scriptsize]{$\kappa=0$}},
    },
    align=left,
  },
  [{Testing\\Consumer\\$t=0$}
    [{t=1\\$E[u(c',l',\eta_{k,1})|K]$}
      [{$E[u(c',l',\eta_k)|K]$}
      ]
      [{$E[u(c',l',\eta_{k,1})|\eta_0]$}
      ]
    ]
    [{$E[u(c,l,\eta_1)|K]$}
    ]
  ]
\end{forest}
\begin{forest}
  align=center,
  for tree={%
    grow'=east,
    calign=midpoint,
    parent anchor=children,
    child anchor=parent,
    anchor=child anchor,
    l sep'+=25pt,
    s sep'+=20pt,
    edge+={-Stealth},
    font=\sffamily,
  },
  for descendants={%
    if n=1{edge label={node[above,midway,sloped,font=\scriptsize]{$\kappa=1$}}}{
      edge label={node[below,midway,sloped,font=\scriptsize]{$\kappa=0$}},
    },
    align=left,
  },
  before typesetting nodes={%
    tikz+={\coordinate (c) at ([yshift=5pt]current bounding box.north);},
    tempcounta/.max={level}{tree},
    for nodewalk={%
      do while={>OR<{level}{tempcounta}}{fake=next node},
      current and ancestors
    }{%
      tikz+/.process={Ow{level}{%
          \node [anchor=base] at ( |- c) {$t=#1$};
        }
      },
    },
  },
  [{Testing\\Consumer}
    [{$E[u(c',l',\eta_{k,1})|K]$}
      [{$E[u(c',l',\eta_k)|K]$}
      ]
      [{$E[u(c',l',\eta_{k,1})|\eta_0]$}
      ]
    ]
    [{$E[u(c,l,\eta_1)|K]$}
    ]
  ]
\end{forest}

\end{document}

相关内容