1

Since my real tree is too wide, I want to move and resize the node label -X_0+X_1>0 so that the red box at the end gets smaller because it dynamically adjusts its size to the nodes.

The arrows and extra text in the image are just for clarification (sorry if it confuses). If you know a solution to resize all node labels that would be even better, but in addition I only want to move the label of this specific node.

I want to format the content of a node, i.e. move and scale the label of the node (not an edge) in the forest package with decision tree forest style.

\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}

Forest node label format example

The label should not be from an edge, because in my real tree there are separate edge labels and the respective label should logically be attached to the node.

Not filling in the node content and drawing a separate tikz node is not an option, as the red dotted box is dynamically aligned with the node and thus the node content.

If you adjust the position of the node, the whole node will be moved. However, in the forest documentation there is an option to format only the node content. However, I have not been able to find an example of this to make sure that this is the correct option, nor figured I out how to use it.

Is it possible to move and scale only the content of the node, but keep the logical connection with the node, so that the anchors on the nodes still refer to the content of the node?

7
  • Welcome to TeX.SX! It might be better to explain a bit more about what you want to achieve in the end. You try to adapt an example from the manual to your needs, which might not really fit your needs. So, what you're after is an edge label which is included in the red dashed box? Mar 24 at 14:06
  • Since my real tree is too wide, I want to move and resize the node label "-X_0+X_1>0" so that the red box at the end gets smaller because it dynamically adjusts its size to the nodes.
    – vielfarbig
    Mar 24 at 14:25
  • So, you only want to shift and resize this one label? Mar 24 at 14:37
  • Yes, the arrows and extra text in the image are just for clarification, but it seems to confuse more (sorry for that). If you know a solution to resize all node labels that would be even better, but I only want to move the label of this specific node.
    – vielfarbig
    Mar 24 at 14:41
  • The problem is that this is actually not a label, but it is the node. You cannot easily shift the node without distorting the tree. So, I am not sure that what you're after is acutally possible. You could resize the font using font=\scriptsize for example, or you can change the anchor. Mar 24 at 15:19

1 Answer 1

1

What looks like the label of the node is actually the node. Therefore, shifting does not work, at least not without distorting the tree. In my opinion, it might be best to add another node which you can give a name. You can then add this name to the list of nodes of the fit option.

For example, you could do the following:

\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}

enter image description here


If you actually want to shift and resize the label of the edge, this can be easily done like this:

\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}

enter image description here

1
  • Thank you very much! Your first solution is the one I will actually use because all edges already have labels. But your suggestion to use the edge labels to move them more easily by forest and make them look like the node labels is also interesting and worth considering if there is such a problem. I think I could also use the second variant and add additional edge labels as nodes to the respective anchors.
    – vielfarbig
    Mar 24 at 16:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .