如何自动标记森林中的节点?

如何自动标记森林中的节点?

我尝试到处寻找解决方案,但是要么没能找到它,要么它根本不存在。

我有一片森林,我想给每个节点添加一个标签,这样“Deadbeef”将被标记为 1,“Something”将被标记为 1.1,“else”将被标记为 1.2,然后它们的子节点被标记为 1.1.1 等等。

我知道我可以使用\label命令来做到这一点,但是我很懒而且我的树在长大,所以我不想继续简单地编号。

\begin{forest}
for tree={
    grow'=0,
    align=center, 
    base=bottom
},
[Deadbeef
    [Something
        [Generic\\node]
        [Other\\ generic node]
        [That one\\there]        
    ]
    [Else
        [Client\\Side
            [Become\\data]
            [Bathe in\\data]
        ]
        [Server\\Side
            [One with\\data]
            [Meta\\data
                [Update\\main service]
                [Update\\database]
            ]
        ]
    ]
    [Kindof
        [You are\\very kind]
        [How much\\for the lot?]
        [Really not\\serious]
    ]
]
\end{forest}

在此处输入图片描述在此处输入图片描述

我的绘画工作并不出色,但我希望它能描绘出我想要实现的想法。理想的解决方案应该采用任意大小的树并相应地标记节点(如果我没有说清楚的话)。


编辑 1:标签应该一直到节点。我知道 cfr 的答案,但是级别是手动指定的。我不知道一棵树会有多少个级别,所以我希望这能自动完成。

答案1

请始终提供可编译的代码。 它极大地减少了不确定性,增加了问题得到解答的可能性(a)和(b)以真正帮助提问者的方式,更加高效,对新用户更有帮助,并且通常使生活变得更加轻松。

猜测如何完成你的代码,我也不清楚你是否真的意味着标签或者如果你想改变节点内容。我假设是后者,因为据我所知,标签在这里没有什么意义。也就是说,我认为目标是这样的:

成果/预期目标

如果是这样:

\begin{forest}
  for tree={
    grow'=0,
    align=center, 
    base=bottom
  },
  before typesetting nodes={
    for tree breadth-first={
      if level=0{temptoksa=1}{
        temptoksa/.option=n,
        for nodewalk={
          while nodewalk valid={u}{u,+temptoksa=.,if level=0{+temptoksa=1}{+temptoksa/.option=n}}
        }{},
      },
      content/.process={ROw2{temptoksa}{content}{#1 #2}}
    }
  }
  [Deadbeef
    [Something
      [Generic\\node]
      [Other\\ generic node]
      [That one\\there]        
    ]
    [Else
      [Client\\Side
        [Become\\data]
        [Bathe in\\data]
      ]
      [Server\\Side
        [One with\\data]
        [Meta\\data
          [Update\\main service]
          [Update\\database]
        ]
      ]
    ]
    [Kindof
      [You are\\very kind]
      [How much\\for the lot?]
      [Really not\\serious]
    ]
  ]
\end{forest}

顺便说一句,这棵树对我来说看起来不太清晰,因为边缘通常相对于其子节点随机结束。我可能会选择更像这样的东西:

  for tree={
    grow'=0,
    align=center, 
    font=\sffamily,
  },
  forked edges,
  align middle children,

在树的序言中,align middle children来自自定义库并定义如下:

\forestset{
  align middle child/.style={
    before typesetting nodes={
      if={
        > Ow+P {n children}{isodd(##1)}
      }{
        calign child/.process={
          Ow+n {n children}{(##1+1)/2}
        },
        calign=child edge,
      }{},
    },
  },
  align middle children/.style={
    for tree={align middle child},
  },
}

选择

但这当然只是个人品味的问题。

完整代码:

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\forestset{
  align middle child/.style={
    before typesetting nodes={
      if={
        > Ow+P {n children}{isodd(##1)}
      }{
        calign child/.process={
          Ow+n {n children}{(##1+1)/2}
        },
        calign=child edge,
      }{},
    },
  },
  align middle children/.style={
    for tree={align middle child},
  },
}
\begin{document}
\begin{forest}
  for tree={
    grow'=0,
    align=center, 
    base=bottom
  },
  before typesetting nodes={
    for tree breadth-first={
      if level=0{temptoksa=1}{
        temptoksa/.option=n,
        for nodewalk={
          while nodewalk valid={u}{u,+temptoksa=.,if level=0{+temptoksa=1}{+temptoksa/.option=n}}
        }{},
      },
      content/.process={ROw2{temptoksa}{content}{#1 #2}}
    }
  }
  [Deadbeef
    [Something
      [Generic\\node]
      [Other\\ generic node]
      [That one\\there]        
    ]
    [Else
      [Client\\Side
        [Become\\data]
        [Bathe in\\data]
      ]
      [Server\\Side
        [One with\\data]
        [Meta\\data
          [Update\\main service]
          [Update\\database]
        ]
      ]
    ]
    [Kindof
      [You are\\very kind]
      [How much\\for the lot?]
      [Really not\\serious]
    ]
  ]
\end{forest}
\begin{forest}
  for tree={
    grow'=0,
    align=center, 
    font=\sffamily,
  },
  forked edges,
  align middle children,
  before typesetting nodes={
    for tree breadth-first={
      if level=0{temptoksa=1}{
        temptoksa/.option=n,
        for nodewalk={
          while nodewalk valid={u}{u,+temptoksa=.,if level=0{+temptoksa=1}{+temptoksa/.option=n}}
        }{},
      },
      content/.process={ROw2{temptoksa}{content}{#1 #2}}
    }
  }
  [Deadbeef
    [Something
      [Generic\\node]
      [Other\\ generic node]
      [That one\\there]        
    ]
    [Else
      [Client\\Side
        [Become\\data]
        [Bathe in\\data]
      ]
      [Server\\Side
        [One with\\data]
        [Meta\\data
          [Update\\main service]
          [Update\\database]
        ]
      ]
    ]
    [Kindof
      [You are\\very kind]
      [How much\\for the lot?]
      [Really not\\serious]
    ]
  ]
\end{forest}
\end{document}

答案2

这几乎完全是从@cfr 的回答很好,如果@cfr 发表答案,我很乐意撤回我的帖子。我相信@cfr 也能避免丑闻\addone。所以这只是以防万一……

\documentclass[border=3.14mm]{standalone}
\usepackage[edges]{forest}
\newcommand{\addone}[1]{\pgfmathtruncatemacro{\tmp}{#1+1}\tmp}
\begin{document}

\begin{forest}
set root label/.style={
    content/.wrap 2 pgfmath args={\addone{##1}. ##2}{n()}{content()},
  },
set node label/.style={
    content/.wrap 3 pgfmath args={\addone{##1}.##2 ##3}{n("!r")}{n()}{content()},
  },
set node super label/.style={
    content/.wrap 4 pgfmath args={\addone{##1}.##2.##3  ##4}{n("!rr")}{n("!u")}{n()}{content()},
  },
before typesetting nodes={
    for tree={
      if level=0{
        set root label,
      }{
        if level=1{
          set node label,
        }{
          if level=2{
            set node super label,
          }{
          },
        },
      },
    },
  },
for tree={
    grow'=0,
    align=center, 
    base=bottom
},
[Deadbeef
    [Something
        [Generic\\node]
        [Other\\ generic node]
        [That one\\there]        
    ]
    [Else
        [Client\\Side
            [Become\\data]
            [Bathe in\\data]
        ]
        [Server\\Side
            [One with\\data]
            [Meta\\data
                [Update\\main service]
                [Update\\database]
            ]
        ]
    ]
    [Kindof
        [You are\\very kind]
        [How much\\for the lot?]
        [Really not\\serious]
    ]
]
\end{forest}
\end{document}

在此处输入图片描述

相关内容