如何格式化(移动和缩放)森林决策树中的唯一节点标签

如何格式化(移动和缩放)森林决策树中的唯一节点标签

由于我的真实树太宽,我想移动并调整节点标签的大小-X_0+X_1>0,以便末尾的红色框变小,因为它会根据节点动态调整其大小。

图片中的箭头和额外文字只是为了说明(如果造成混淆,请见谅)。如果您知道调整所有节点标签大小的解决方案,那就更好了,但除此之外,我只想移动这个特定节点的标签。

我想格式化一个节点的内容,即使用决策树森林风格移动和缩放森林包中的节点(不是边)的标签。

\forestset{
    declare toks={elo}{}, % Edge Label Options
    anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
    dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
    dot/.default=2pt,
    decision edge label/.style n args=3{
        edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}}
    },
    decision/.style={if n=1
        {decision edge label={left}{east}{#1}}
        {decision edge label={right}{west}{#1}}
    },
    decision tree/.style={
        for tree={
            s sep=0.5em,l=8ex,
            if n children=0{anchors=north}{
                if n=1{anchors=south east}{anchors=south west}},
            math content,
        },
        anchors=south, outer sep=2pt,
        dot=3pt,for descendants=dot,
        delay={for descendants={split option={content}{;}{content,decision}}},
    }
}

% Source: https://tex.stackexchange.com/a/445950/279450
\tikzset{
rdot/.style = {circle, fill, minimum size=#1,
              inner sep=0pt, outer sep=0pt, draw=black, fill=black},
rdot/.default = 4pt % size of the circle diameter 
}

\begin{forest} decision tree
    [{},plain content,tikz={
            \node [draw,red,loosely dashed, fit=()(!1)(!2)]{};
            \node [rdot] at (!0.anchor) {};
        }
        [{$-X_0+X_1>0$};{},plain content,elo={yshift=4pt}
            [{};{},plain content]
            [{};{},plain content]
        ] {}
        [{};{},plain content
            [{};{},plain content]
            [{};{},plain content]
        ] {}
    ] {}
\end{forest}

森林节点标签格式示例

标签不应来自边,因为在我的真实树中存在单独的边标签,并且相应的标签应在逻辑上附加到节点。

不填写节点内容并绘制单独的 tikz 节点不是一个选择,因为红色虚线框与节点动态对齐,因此也与节点内容动态对齐。

如果调整节点的位置,整个节点都会移动。但是,在森林文档中有一个选项可以仅格式化节点内容。但是,我无法找到一个示例来确保这是正确的选项,也不知道如何使用它。

是否可以仅移动和缩放节点的内容,但保持与节点的逻辑连接,以便节点上的锚点仍然引用节点的内容?

答案1

看起来像节点标签的东西实际上是节点。因此,移动不起作用,至少在不扭曲树的情况下不起作用。在我看来,最好添加另一个可以命名的节点。然后您可以将此名称添加到选项的节点列表中fit

例如,您可以执行以下操作:

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

\begin{document}

\forestset{
    declare toks={elo}{}, % Edge Label Options
    anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
    dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
    dot/.default=2pt,
    decision edge label/.style n args=3{
        edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}},
    },
    decision/.style={if n=1
        {decision edge label={left}{east}{#1}}
        {decision edge label={right}{west}{#1}},
    },
    node label/.style n args=3{
        append after command={
            \pgfextra{
                \node[name=#1, font=\scriptsize] at ([shift={#2}]) {#3};
            }
        }
    },
    decision tree/.style={
        for tree={
            s sep=0.5em,l=8ex,
            if n children=0{anchors=north}{
                if n=1{anchors=south east}{anchors=south west}},
            math content,
        },
        anchors=south, outer sep=2pt,
        dot=3pt,for descendants=dot,
        delay={for descendants={split option={content}{;}{content,decision}}},
    }
}

% Source: https://tex.stackexchange.com/a/445950/279450
\tikzset{
rdot/.style = {circle, fill, minimum size=#1,
              inner sep=0pt, outer sep=0pt, draw=black, fill=black},
rdot/.default = 4pt % size of the circle diameter 
}

\begin{forest} decision tree
    [{},plain content,tikz={
            \node [draw,red,loosely dashed, fit=()(!1)(!2)(l1)]{};
            \node [rdot] at (!0.anchor) {};
        }
        [{};{},plain content,node label={l1}{(-0.75,0.5)}{$-X_0+X_1>0$}
            [{};{},plain content]
            [{};{},plain content]
        ] {}
        [{};{},plain content
            [{};{},plain content]
            [{};{},plain content]
        ] {}
    ] {}
\end{forest}

\end{document}

在此处输入图片描述


如果您确实想移动和调整边缘标签的大小,可以很容易地这样做:

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

\begin{document}

\forestset{
    declare toks={elo}{}, % Edge Label Options
    anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
    dot/.style={tikz+={\fill (.child anchor) circle[radius=#1];}},
    dot/.default=2pt,
    decision edge label/.style n args=3{
        edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$\unexpanded{#3}$}},
    },
    decision/.style={if n=1
        {decision edge label={left}{east}{#1}}
        {decision edge label={right}{west}{#1}},
    },
    decision tree/.style={
        for tree={
            s sep=0.5em,l=8ex,
            if n children=0{anchors=north}{
                if n=1{anchors=south east}{anchors=south west}},
            math content,
        },
        anchors=south, outer sep=2pt,
        dot=3pt,for descendants=dot,
        delay={for descendants={split option={content}{;}{content,decision}}},
    }
}

% Source: https://tex.stackexchange.com/a/445950/279450
\tikzset{
rdot/.style = {circle, fill, minimum size=#1,
              inner sep=0pt, outer sep=0pt, draw=black, fill=black},
rdot/.default = 4pt % size of the circle diameter 
}

\begin{forest} decision tree
    [{},plain content,tikz={
            \node [draw,red,loosely dashed, fit=()(!1)(!2)(l1)]{};
            \node [rdot] at (!0.anchor) {};
        }
        [{};{-X_0+X_1>0},plain content,elo={name=l1, font=\scriptsize}
            [{};{},plain content]
            [{};{},plain content]
        ] {}
        [{};{},plain content
            [{};{},plain content]
            [{};{},plain content]
        ] {}
    ] {}
\end{forest}

\end{document}

在此处输入图片描述

相关内容