如何自動標記森林節點,將標籤作為前綴添加到節點內容一次?

如何自動標記森林節點,將標籤作為前綴添加到節點內容一次?

我試圖弄清楚如何forest在圖中標記節點,例如圖中所示的節點這個問題自動地。所以,我們的想法是:

  • level 0:節點內容應以 ie 為前綴O.,如果指定的內容為P0,則該節點應以內容排版O. P0
  • O.n等級 1:內容應以wheren為子級編號為前綴,例如,如果第一級節點指定為P1P3,則應將它們排版為O.1 P1O.2 P3
  • 等級 2:內容應以前綴O.n.mwheren是父節點的子編號,m是節點的子編號,例如,如果P1有子節點P2,則後者應顯示為O.1.1 P2,如果P3有子節點P4,則後者應排版為O.2.1 P4

0級沒問題。我可以使用content={O. #1}.我還可以建立相關標籤並將其包含在等級 1 和等級 2 的節點中。無論我做什麼,我最終要么沒有前綴,要么有亂碼或空前綴,或者有多個正確前綴的副本。

我認為這是我最接近的:

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}

\forestset{
  tree node/.style = {align=center, inner sep=2pt, rounded corners = 2pt, text centered, font=\sffamily, rectangle, draw=black},
}
\begin{forest}
  for tree={
    parent anchor=south,
    child anchor=north,
    tree node,
    l sep+=5pt,
    edge path={
      \noexpand\path [-{Stealth[]}, \forestoption{edge}, thick]
        (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
    },
  },
  set node label/.style={
    content/.wrap 2 pgfmath args={O.##1 ##2}{n()}{content()},
  },
  set node super label/.style={
    content/.wrap 3 pgfmath args={O.##1.##2 ##3}{n("!u")}{n()}{content()},
  },
  before typesetting nodes={
    for tree={
      where level=0{
        content={O. #1},
      }{
        where level=1{
          set node label,
        }{
          where level=2{
            set node super label,
          }{
          },
        },
      },
    },
  }
  [P0
    [P1
      [P2
      ]
    ]
    [P3
      [P4
      ]
    ]
  ]
  ;
\end{forest}
\end{document}

這正確地構造了前綴,但它太頻繁地將它們添加到節點!

標籤太多了!

我還嘗試將原始內容儲存在巨集中,然後在content.然而,在這種情況下,我就完全失去了原來的內容。

答案1

將所有wheres 替換為ifs。

where定義為where/.style n args={3}{for tree={if={#1}{#2}{#3}}},因此它隱式包含一個for tree循環。

相關內容