具有多行节点标签的 TikZ 树对齐

具有多行节点标签的 TikZ 树对齐

我正在尝试在 tikZ 中创建一个树结构,它看起来离我想要的不太远,但是存在一些问题:

  1. 标签是多行的,这似乎有点破坏了边缘的对齐。边缘应该以更居中的方式延伸。
  2. 树中仍有很多重叠,如果我再多玩一下兄弟距离,也许可以做到这一点?我只是还不确定如何做到这一点,如果有更简单的方法,我很高兴听到。
  3. 为了每个叶节点我想发表一条文字标签下方。对于这一点,我还不确定该如何做,因为我想在叶子标签周围画一个圆圈(以叶子标签为例R\\(0, 0),但文本应该在圆圈外面,在圆圈下面。

这就是树的当前样子。对齐/重叠问题仍然非常明显:

在此处输入图片描述

源代码(可通过 编译pdflatex)在这里

\documentclass{article}
\usepackage[usenames]{color}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\pagestyle{empty}             % do not remove
% The settings below are copied from fullpage.sty
\setlength{\textwidth}{\paperwidth}
\addtolength{\textwidth}{-3cm}
\setlength{\oddsidemargin}{1.5cm}
\addtolength{\oddsidemargin}{-2.54cm}
\setlength{\evensidemargin}{\oddsidemargin}
\setlength{\textheight}{\paperheight}
\addtolength{\textheight}{-\headheight}
\addtolength{\textheight}{-\headsep}
\addtolength{\textheight}{-\footskip}
\addtolength{\textheight}{-3cm}
\setlength{\topmargin}{1.5cm}
\addtolength{\topmargin}{-2.54cm}
\usepackage{tikz}
\usepackage{caption}

\begin{document}
\usetikzlibrary{graphs,graphs.standard}
\usetikzlibrary{decorations, decorations.text,backgrounds}

\begin{figure}
\centering
\begin{tikzpicture}[
level distance=20mm,
level 1/.style={sibling distance=50mm},
level 2/.style={sibling distance=30mm},
level 3/.style={sibling distance=20mm},
level 4/.style={sibling distance=10mm},
scale=1.4,transform shape]

\node[label={[align=center]X\\(2, 2)}] {}
  child {node[label={[align=center]Y\\(0, 2)}] {}
    child {node[label={[align=center]X\\(0, 1)}] {}
       child {node[label={[align=center]Y\\(0, 0)}] {}}
    }
    child {node[label={[align=center]X\\(0, 0)}] {}}
  }
  child {node[label={[align=center]Y\\(1, 2)}] {}
    child {node[label={[align=center]X\\(0, 2)}] {}
        child {node[label={[align=center]Y\\(0, 0)}] {}}
        child {node[label={[align=center]Y\\(0, 1)}] {}
            child {node[label={[align=center]X\\(0, 0)}] {}}
        }
    }
    child {node[label={[align=center]X\\(1, 1)}] {}
        child {node[label={[align=center]Y\\(0, 1)}] {}
           child {node[label={[align=center]X\\(0, 0)}] {}}
        }
        child {node[label={[align=center]Y\\(1, 0)}] {}
           child {node[label={[align=center]X\\(0, 0)}] {}}
        }
    }
    child {node[label={[align=center]X\\(1, 0)}] {}
        child {node[label={[align=center]Y\\(0, 0)}] {}}
    }
  };

\end{tikzpicture}
\caption*{\huge Question 2 a), the simplified game tree, since we can assume $(a, b) = (b, a)$.}
\end{figure}
\end{document}

答案1

为您指明正确方向的东西——改编自——控制 tikz 树

在此处输入图片描述

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\tikzset{
    treenode/.style = {shape=rectangle, rounded corners,
    draw, align=center, font=\scriptsize},
    root/.style     = {treenode},
    env/.style      = {treenode},
    every node/.style       = {font=\tiny},
}
\begin{document}
    \begin{tikzpicture}
        [
        grow                    = down,
        level 1/.style          = {sibling distance=5cm},
        level 2/.style          = {sibling distance=2.5cm},
        level 3/.style          = {sibling distance=1.5cm},
        level distance          = 1.2cm,
        edge from parent/.style = {draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}},
        ]
        \node [root] {X\\22}
        child { node [env] {Y\\02}
            child { node [env] {X\\01}
                child{ node [env,label=below:Leaf 1] {Y\\00}}
            }
            child{ node [env,label=below:Leaf 2] {X\\01}
            }
        }
        child { node [env] {Y\\12}
            child{ node [env] {X\\12    }
                child{ node [env,label=below:Leaf 4] {Y\\00}
}
                child{ node [env,label=below:Leaf 5] {Y\\01}
}
            }
            child{ node [env] {X\\11}
                child { node [env] {Y\\01}
                    child{ node [env,label=below:Leaf 6] {X\\00}}
}
                child{ node [env,] {Y\\01}
                                    child{ node [env,label=below:Leaf 7] {X\\00}}}
            }
            child{ node [env] {X\\10}
                child{ node [env,label=below:Leaf 8] {Y\\00}
}
            }
        };
    \end{tikzpicture}
\end{document}

答案2

另外,您可以使用forest,这将使您更轻松地格式化叶子。

在此处输入图片描述

\documentclass{article}

\usepackage{forest}

\begin{document}

\begin{forest}
for tree={
    parent anchor=south, child anchor=north,
    align=center, 
    s sep=6mm, l sep=8mm,
    if n children=0{draw, circle, minimum size=8mm, inner sep=0pt, fit=band}{}
}
[{X\\(2,2)}
    [{Y\\(0,2)}
        [{X\\(0,1)}
            [{Y\\(0,0)}, label=below:text]
        ]
        [{X\\(0,0)}, label=below:text]
    ]
    [{Y\\(1,2)}
        [{X\\(0,2)}
            [{Y\\(0,0)}, label=below:text]
            [{Y\\(0,1)}
                [{X\\(0,0)}, label=below:text]
            ]
        ]
        [{X\\(1,1)}
            [{Y\\(0,1)}
                [{X\\(0,0)}, label=below:text]
            ]
            [{Y\\(1,0)}
                [{X\\(0,0)}, label=below:text]
            ]
        ]
        [{X\\(1,0)}
            [{Y\\(0,0)}, label=below:text]
        ]
    ]
]
\end{forest}

\end{document}

答案3

istgame

添加:使用\xtShowTerminalNodes\istbt

在此处输入图片描述

\documentclass[border=5mm]{standalone}

\usepackage{istgame}

\usepackage{makecell}
\NewDocumentCommand\mline{O{c}+m}{\makecell[#1]{#2}}

\begin{document}

\begin{istgame}[font=\scriptsize]
\setistmathTF*000 % all labels in text mode
\tikzset{oval node/.style={ellipse node,draw,circle,minimum size=2.8em}}
\xtShowTerminalNodes[oval node]  %%% show terminal nodes
% tree
\xtdistance{20mm}{70mm}
\istrooto(0){\mline{X\\(2,2)}}           \istb[blue,thick]  \istb[red,very thick]   \endist
% left part: blue
\xtdistance{20mm}{30mm}
\istrooto(L)(0-1){\mline{Y\\$(0,2)$}}    \istb \istbt{}{\mline{X\\$(0,0)$}}[center] \endist
  \xtPayoff*(L-2){\mline{text\\below}}[below=1.3em]
\istrooto(L1)(L-1){\mline{X\\$(0,1)$}}   \istbt{}{\mline{Y\\$(0,0)$}}[center]       \endist
  \xtPayoff*(L1-1){\mline{text\\outside}}[below=1.3em]
% right part: red
\xtdistance{20mm}{30mm}
\istrooto(R)(0-2){\mline{Y\\$(1,2)$}}    \istb \istb \istb \endist
\xtdistance{20mm}{15mm}
\istrooto(R1)(R-1){\mline{X\\$(0,2)$}}   \istbt{}{\mline{Y\\$(0,0)$}}[center] \istb \endist
  \xtPayoff*(R1-1){text 1}[below=1.3em]
\istrooto(R2)(R-2){\mline{X\\$(1,1)$}}   \istb                                \istb \endist
\istrooto(R3)(R-3){\mline{X\\$(1,0)$}}   \istbt{}{\mline{Y\\$(0,0)$}}[center]       \endist
  \xtPayoff*(R3-1){text 2}[below=1.3em]
\istrooto(R12)(R1-2){\mline{Y\\$(0,1)$}} \istbt{}{\mline{X\\$(0,0)$}}[center]       \endist
  \xtPayoff*(R12-1){text 3}[below=1.3em]
\istrooto(R21)(R2-1){\mline{Y\\$(0,1)$}} \istbt{}{\mline{X\\$(0,0)$}}[center]       \endist
  \xtPayoff*(R21-1){text 4}[below=1.3em]
\istrooto(R22)(R2-2){\mline{Y\\$(1,0)$}} \istbt{}{\mline{X\\$(0,0)$}}[center]       \endist
  \xtPayoff*(R22-1){text 5}[below=1.3em]
\end{istgame}

\end{document}

原来的:

在此处输入图片描述

\documentclass[border=5mm]{standalone}

\usepackage{istgame}

\usepackage{makecell}
\NewDocumentCommand\mline{O{c}+m}{\makecell[#1]{#2}}

\begin{document}

\begin{istgame}[font=\scriptsize]
\setistmathTF*000 % all labels in text mode
\tikzset{oval node/.style={ellipse node,draw,circle}}
% tree: root
\xtdistance{20mm}{70mm}
\istrooto(0){\mline{X\\(2,2)}} 
  \istb[blue,thick]
  \istb[red,very thick]
  \endist
% left part: blue
\xtdistance{20mm}{30mm}
\istrooto(L)(0-1){\mline{Y\\$(0,2)$}}      \istb \istb \endist
  \xtOwner*(L){\mline{text\\left-side}}[left=1.3em,blue]
\istrooto(L1)(L-1){\mline{X\\$(0,1)$}}     \istb       \endist
\istrooto(L2)(L-2){\mline{X\\$(0,0)$}}     \endist
  \xtPayoff*(L2){\mline{text\\below}}[below=1.3em]
\istrooto(L11)(L1-1){\mline{Y\\$(0,0)$}}   \endist
  \xtPayoff*(L11){\mline{text\\outside}}[below=1.3em]
% right part: red
\xtdistance{20mm}{30mm}
\istrooto(R)(0-2){\mline{Y\\$(1,2)$}}      \istb \istb \istb \endist
\xtdistance{20mm}{15mm}
\istrooto(R1)(R-1){\mline{X\\$(0,2)$}}     \istb \istb \endist
\istrooto(R2)(R-2){\mline{X\\$(1,1)$}}     \istb \istb \endist
\istrooto(R3)(R-3){\mline{X\\$(1,0)$}}     \istb       \endist
\istrooto(R11)(R1-1){\mline{Y\\$(0,0)$}}   \endist
  \xtPayoff*(R11){text 1}[below=1.3em]
\istrooto(R12)(R1-2){\mline{Y\\$(0,1)$}}   \istb       \endist
\istrooto(R21)(R2-1){\mline{Y\\$(0,1)$}}   \istb       \endist
\istrooto(R22)(R2-2){\mline{Y\\$(1,0)$}}   \istb       \endist
\istrooto(R31)(R3-1){\mline{Y\\$(0,0)$}}   \endist
  \xtPayoff*(R31){text 2}[below=1.3em]
\istrooto(R121)(R12-1){\mline{X\\$(0,0)$}} \endist
  \xtPayoff*(R121){text 3}[below=1.3em]
\istrooto(R211)(R21-1){\mline{X\\$(0,0)$}} \endist
  \xtPayoff*(R211){text 4}[below=1.3em]
\istrooto(R221)(R22-1){\mline{X\\$(0,0)$}} \endist
  \xtPayoff*(R221){text 5}[below=1.3em]
\end{istgame}

\end{document}

相关内容