在 TikZ 二叉树中制作 1 个细列

在 TikZ 二叉树中制作 1 个细列

我有一个二项式树,箭头指向后方。目前,我有一些指向最左侧节点(不可见)的短箭头。第一列占用的视觉空间很小,但它将我的tikz格子向右推了整整一列的宽度。

怎么修?

     \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{matrix,fit,backgrounds}

    \begin{document}



       \begin{tikzpicture}[>=stealth,sloped]
          \matrix (tree) [%
            matrix of nodes,
            minimum size=1cm,
            column sep=2.7cm,
            row sep=0.4cm,
          column 4/.style={nodes={draw,fill=cyan!30,rounded corners}},
          ]
          {
          &                    &                                               &  $^\tau S_0$     \\
          &                    &  $R^{-1}(p (_\tau S_0) + (1-p) (_\tau S_2))$  &              \\
          & ...                &                                               &  $^\tau S_1$     \\
    $  $   &                    &  $R^{-1}(p (_\tau S_1) + (1-p) (_\tau S_2))$  &              \\
          & ...                &                                               &  $^\tau S_2$     \\
          &                    &  $R^{-1}(p (_\tau S_2) + (1-p) (_\tau S_3))$  &              \\
          &                    &                                               &  $^\tau S_3$     \\
          };
          \draw[<-,shorten <=2cm,double,double distance=1pt] (tree-4-1) -- (tree-3-2) node [midway,above] {};
          \draw[<-,shorten <=2cm,double,double distance=1pt] (tree-4-1) -- (tree-5-2) node [midway,above] {};

          \draw[<-,double,double distance=1pt] (tree-3-2) -- (tree-2-3) node [midway,above] {};
          \draw[<-,double,double distance=1pt] (tree-3-2) -- (tree-4-3) node [midway,below] {};
          \draw[<-,double,double distance=1pt] (tree-5-2) -- (tree-4-3) node [midway,above] {};
          \draw[<-,double,double distance=1pt] (tree-5-2) -- (tree-6-3) node [midway,below] {};
          \draw[<-,double,double distance=1pt] (tree-2-3) -- (tree-1-4) node [midway,above] {};
          \draw[<-,double,double distance=1pt] (tree-2-3) -- (tree-3-4) node [midway,below] {};
          \draw[<-,double,double distance=1pt] (tree-4-3) -- (tree-3-4) node [midway,above] {};
          \draw[<-,double,double distance=1pt] (tree-4-3) -- (tree-5-4) node [midway,below] {};
          \draw[<-,double,double distance=1pt] (tree-6-3) -- (tree-5-4) node [midway,above] {};
          \draw[<-,double,double distance=1pt] (tree-6-3) -- (tree-7-4) node [midway,below] {};

        \end{tikzpicture}

    \end{document}

答案1

您的column sep选项对第一列也有效。因此您需要覆盖它。此外,如果您的所有节点都涉及数学运算符,您可以简单地使用,matrix of math nodes这样您就不需要$对条目使用符号了。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,fit,backgrounds}

\begin{document}



   \begin{tikzpicture}[>=stealth,sloped,mypath/.style={<-,double,double distance=1pt}]
      \matrix (tree) [%
        matrix of nodes,
        minimum size=1cm,
        column sep=2.7cm,
        row sep=0.4cm,
      column 4/.style={nodes={draw,fill=cyan!30,rounded corners}},
      column 1/.style={nodes={minimum size=3mm,inner sep=1pt,outer sep=0},column sep=3mm},
      ]
      {
      &                    &                                               &$^\tau S_0$     \\
      &                    &  $R^{-1}(p (_\tau S_0) + (1-p) (_\tau S_2))$  &              \\
      & ...                &                                               &  $^\tau S_1$     \\
$  $  &                    &  $R^{-1}(p (_\tau S_1) + (1-p) (_\tau S_2))$  &              \\
      & ...                &                                               &  $^\tau S_2$     \\
      &                    &  $R^{-1}(p (_\tau S_2) + (1-p) (_\tau S_3))$  &              \\
      &                    &                                               &  $^\tau S_3$     \\
      };
      \draw[mypath,shorten <=2mm] (tree-4-1) -- (tree-3-2) node [midway,above] {};
      \draw[mypath,shorten <=2mm] (tree-4-1) -- (tree-5-2) node [midway,above] {};

      \draw[mypath] (tree-3-2) -- (tree-2-3) node [midway,above] {};
      \draw[mypath] (tree-3-2) -- (tree-4-3) node [midway,below] {};
      \draw[mypath] (tree-5-2) -- (tree-4-3) node [midway,above] {};
      \draw[mypath] (tree-5-2) -- (tree-6-3) node [midway,below] {};
      \draw[mypath] (tree-2-3) -- (tree-1-4) node [midway,above] {};
      \draw[mypath] (tree-2-3) -- (tree-3-4) node [midway,below] {};
      \draw[mypath] (tree-4-3) -- (tree-3-4) node [midway,above] {};
      \draw[mypath] (tree-4-3) -- (tree-5-4) node [midway,below] {};
      \draw[mypath] (tree-6-3) -- (tree-5-4) node [midway,above] {};
      \draw[mypath] (tree-6-3) -- (tree-7-4) node [midway,below] {};

    \end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容