4

I am trying to modify the answer provided by @cfr in this question. However, there are strange content "height" in my result, see each box. Could anyone help me resolve this?

My code is

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{amsmath}
\usepackage{forest}
\useforestlibrary{edges}
\tikzset{%
  >=stealth,
  parent node/.style={%
    rectangle split,
    rectangle split parts=2,
    align=center,
    text width=2.5cm,
    draw,
    node distance=1cm and 1cm
  }
}
\begin{document}
\begin{forest}
  %forked edges,
  for tree={
    draw,
    inner xsep=1pt,
    edge={-Stealth},
    l sep+=20pt,
    %fork sep+=10pt,
  },
  before typesetting nodes={
    where level=0{}{
      split option={content}{:}{temptoksa,temptoksb},
      content/.wrap 2 pgfmath args={#1\\\hline #2}{temptoksa}{temptoksb},
      if={(n()==1)&&(level()>1)}{%
        edge label/.wrap value={%
          node [midway, left, font=\scriptsize] {#1}%
        },
      }{%
        edge label/.wrap value={%
          node [midway, right, font=\scriptsize] {#1}%
        },
      },
    },
  }
  [$ILO$
    [{}, edge label={solve as continuous problem}
      [{$x_1^*=1,x_2^*=2.166$}, edge label={$x_1\geq 2$}
       [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_1\leq 1$}
         [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_2\leq 2$}
         ]
       ]
       [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_2\geq 7$}
       ]
     ]
      [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_1\geq 1$}
      ]
    ]
  ]
\end{forest}
\end{document}

And my result is enter image description here

2
  • 1
    Welcome to TeX.se, and thanks for posting a minimal document with your question. Your code doesn't compile without errors for me; the output you're seeing is caused by Overleaf pushing through and creating some sort of output no matter what. The error is caused by the \hline in the code. Remove it, and you should get proper output. – Alan Munn Nov 22 '20 at 21:22
  • My answer there expects a tree where most of the nodes have a colon and should be split in two for further processing. – cfr Nov 24 '20 at 1:10
3

I assume that you just need the edge label code, and not the code that splits the node label into two parts with a line between the parts (as in the linked answer.) In which case, this is what you want:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{amsmath}
\usepackage{forest}
\useforestlibrary{edges}
\tikzset{%
  >=stealth,
  parent node/.style={%
    rectangle split,
    rectangle split parts=2,
    align=center,
    text width=2.5cm,
    draw,
    node distance=1cm and 1cm
  }
}
\begin{document}
\begin{forest}
  %forked edges,
  for tree={
    draw,
    inner xsep=1pt,
    edge={-Stealth},
    l sep+=20pt,
    %fork sep+=10pt,
  },
  before typesetting nodes={
    where level=0{}{
      if={(n()==1)&&(level()>1)}{%
        edge label/.wrap value={%
          node [midway, left, font=\scriptsize] {#1}%
        },
      }{%
        edge label/.wrap value={%
          node [midway, right, font=\scriptsize] {#1}%
        },
      },
    },
  }
  [$ILO$
    [{}, edge label={solve as continuous problem}
      [{$x_1^*=1,x_2^*=2.166$}, edge label={$x_1\geq 2$}
       [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_1\leq 1$}
         [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_2\leq 2$}
         ]
       ]
       [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_2\geq 7$}
       ]
     ]
      [{$x_1^*=1,x_2^*=2.1667$}, edge label={$x_1\geq 1$}
      ]
    ]
  ]
\end{forest}
\end{document}

output of code

1
  • Thank you so much! – z-one ng Nov 22 '20 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.