打印树的每一层的深度

打印树的每一层的深度

我正在使用forest包来创建一棵树。但是我想打印树的每一级的深度数字(如下图中的 1 级和 2 级)。

我已经创建了树。其代码如下:

\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Temp}
\author{mjain8 }
\date{May 2017}
\usepackage{forest}

\begin{document}

\begin{forest}
my edge label/.style={
        edge label={node [midway,above,sloped, font=\tiny] {$#1$}}
    },
for tree={%
    l sep=1.5cm,
    s sep=0.2cm,
    minimum height=1.5cm,
    minimum width=1.7cm,
    align=left
    },
    my label/.style={
        label={[anchor=south,font=\scriptsize,red]:#1},
    },
    max label/.style={
        label={[anchor=south,font=\scriptsize]:#1},
    },
    level label/.style={
        label={[font=\scriptsize]left:#1},
    },
    [A,draw,circle,my label={1},max label={maxprofit:0},level label={$i_1$}
        [B,draw,circle,my edge label={\{i_1\}},my label={2},max label={maxprofit:10}
            [D,draw,circle,my edge label={\{i_1,i_2\}},my label={3},max label={maxprofit:40}
                [E,draw,circle,my edge label={\{i_1,i_2,i_3\}},my label={4},max label={maxprofit:80}
                    [F,draw,circle,my edge label={\{i_1,i_2,i_3,i_4\}},my label={5}]
                    [G,draw,circle,my edge label={\{i_1,i_2,i_3\}},my label={6}]
                ]
                [H,draw,circle,my edge label={\{i_1,i_2\}},my label={7}]
            ]
            [I,draw,circle,my edge label={\{i_1\}},my label={8}
                [J5,draw,circle,my edge label={\{i_1,i_3\}},my label={9}
                    [K,draw,circle,my edge label={\{i_1,i_3,i_4\}},my label={10 : F as 17>16}]
                    [L,draw,circle,my edge label={\{i_1,i_3\}},my label={11 : B as 50<80}]
                ]
                [M,draw,circle,my edge label={\{i_1\}},my label={9 : B as 60 < 80}]
            ]
        ]
        [N,draw,circle,my edge label={\{\}},my label={12}
            [O,draw,circle,my edge label={\{i_2\}},my label={13}
                [P,draw,circle,my edge label={\{i_2,i_3\}},my label={14}
                    [Q,draw,circle,my edge label={\{i_2,i_3,i_4\}},my label={15 : F as 17>16}]
                    [R,draw,circle,my edge label={\{i_2,i_3\}},my label={16}]
                ]
                [S,draw,circle,my edge label={\{i_2\}},my label={17}]
            ]
            [T,draw,circle,my edge label={\{\}},my label={18}
                [U,draw,circle,my edge label={\{i_3\}},my label={19}
                    [V,draw,circle,my edge label={\{i_3,i_4\}},my label={20},max label={maxprofit:90}]
                ]
                [W,draw,circle,my edge label={\{\}},my label={21: B as 50<90}]
            ]
        ]
    ]
\end{forest}
\end{document}

我尝试阅读森林文档,但似乎相关文档中没有提及任何内容。

有人可以建议如何做到这一点吗?

答案1

像这样?树上已经有很多标签了,但我在左侧添加了无衬线标签,如图片所示。

级别标签

\documentclass[border=10pt]{standalone}
\usepackage{forest}

\begin{document}
\begin{forest}
  my edge label/.style={
    edge label={node [midway,above,sloped, font=\tiny] {$#1$}}
  },
  for tree={%
    l sep=1.5cm,
    s sep=0.2cm,
    minimum height=1.5cm,
    minimum width=1.7cm,
    align=left,
    circle,
    draw,
  },
  my label/.style={
    label={[anchor=south,font=\scriptsize,red]:#1},
  },
  max label/.style={
    label={[anchor=south,font=\scriptsize]:#1},
  },
  level label/.style={
    label={[font=\scriptsize]left:#1},
  },
  before drawing tree={
    tikz+={
      \node [anchor=mid east, font=\sffamily] (m) at (current bounding box.west |- .mid) {Level 1};
    },
    tempcounta'=1,
    for tree={
      if={% if the current level exceeds the value of tempcounta
        >OR>{level}{tempcounta}%
      }{% then step the count and add the level marker
        tempcounta'+=1,
        tikz+/.process={ Rw {tempcounta} {
            \node [anchor=mid east, font=\sffamily] at (m.east |- .mid) {Level #1};
          }%
        },
      }{},
    },
  },
  [A, my label={1},max label={maxprofit:0},level label={$i_1$}
    [B, my edge label={\{i_1\}},my label={2},max label={maxprofit:10}
      [D, my edge label={\{i_1,i_2\}},my label={3},max label={maxprofit:40}
        [E, my edge label={\{i_1,i_2,i_3\}},my label={4},max label={maxprofit:80}
          [F, my edge label={\{i_1,i_2,i_3,i_4\}},my label={5}]
          [G, my edge label={\{i_1,i_2,i_3\}},my label={6}]
        ]
        [H, my edge label={\{i_1,i_2\}},my label={7}]
      ]
      [I, my edge label={\{i_1\}},my label={8}
        [J5, my edge label={\{i_1,i_3\}},my label={9}
          [K, my edge label={\{i_1,i_3,i_4\}},my label={10 : F as 17>16}]
          [L, my edge label={\{i_1,i_3\}},my label={11 : B as 50<80}]
        ]
        [M, my edge label={\{i_1\}},my label={9 : B as 60 < 80}]
      ]
    ]
    [N, my edge label={\{\}},my label={12}
      [O, my edge label={\{i_2\}},my label={13}
        [P, my edge label={\{i_2,i_3\}},my label={14}
          [Q, my edge label={\{i_2,i_3,i_4\}},my label={15 : F as 17>16}]
          [R, my edge label={\{i_2,i_3\}},my label={16}]
        ]
        [S, my edge label={\{i_2\}},my label={17}]
      ]
      [T, my edge label={\{\}},my label={18}
        [U, my edge label={\{i_3\}},my label={19}
          [V, my edge label={\{i_3,i_4\}},my label={20},max label={maxprofit:90}]
        ]
        [W, my edge label={\{\}},my label={21: B as 50<90}]
      ]
    ]
  ]
\end{forest}
\end{document}

相关内容