5

The following MWE generate a tree diagram (using the forest package) as I like to have:

\documentclass[border=3mm]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style={% Edge Label Style
      font=\footnotesize, inner sep=2pt,
      anchor=south #1, % label position: "ELS=west" or "EL=east"
      pos=0.6}
        }
\newcommand\EL{edge label}
    \begin{forest}
for tree={
    draw, semithick,
    parent anchor=children,
  l sep=13 mm,
  s sep= 1 mm,
    anchor=parent,% <--- work around alignment issue
        },
   EL/.style = {
   before typesetting nodes={%% <--- work around edge labels positioning
    where n=1{%
      edge label/.wrap value={node[ELS=east]{$#1$}}
    }{%
      edge label/.wrap value={node[ELS=west]{$#1$}}
    }
                            }
                }
[Start
    [A, EL=\leq 0.5
        [A1, EL=\leq 0.3
            [A11, EL=\leq 0.7]
            [A12, EL=\geq 0.7]
        ]
        [,phantom,fit=band] % <---
        [A2, EL=\geq 0.3
            [A21, EL=\leq 0.7]
            [A22, EL=\geq 0.7]
         ]
    ]
    [,phantom,fit=band]     % <---
    [B, EL=\geq 0.5
        [B1, EL=\leq 0.3
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
        [,phantom,fit=band] % <---
        [B2, EL=\geq 0.3
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
    ]
]
    \end{forest}
\end{document}

enter image description here

I wonder, if it is possible to obtain the same result without inserting phantom nodes? Also I will appreciate any improvements of the my MWE code.

addendum: one way is insert manually s sep as is done code below:

[Start,s sep=4mm
    [A, EL=\leq 0.5,s sep=4mm 
        [A1, EL=\leq 0.3,s sep=1mm
            [A11, EL=\leq 0.7]
            [A12, EL=\geq 0.7]
        ]
        [A2, EL=\geq 0.3,s sep=1mm 
            [A21, EL=\leq 0.7]
            [A22, EL=\geq 0.7]
         ]
    ]
    [B, EL=\geq 0.5,s sep=4mm 
        [B1, EL=\leq 0.3,s sep=1mm
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
        [B2, EL=\geq 0.3,s sep=1mm
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
    ]
]

considering above solution the question is now: is possible to write a code in fortree preamble, which will replace tose manulaly inserted s sep=...?

4

Yes. Use s sep+.

\documentclass[border=3mm]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style={% Edge Label Style
      font=\footnotesize, inner sep=2pt,
      anchor=south #1, % label position: "ELS=west" or "EL=east"
      pos=0.6}
        }
\newcommand\EL{edge label}
    \begin{forest}
for tree={
    draw, semithick,
    parent anchor=children,
  l sep=13 mm,
  s sep+=2mm,
    anchor=parent,% <--- work around alignment issue
        },
   EL/.style = {
   before typesetting nodes={%% <--- work around edge labels positioning
    where n=1{%
      edge label/.wrap value={node[ELS=east]{$#1$}}
    }{%
      edge label/.wrap value={node[ELS=west]{$#1$}}
    }
                            }
                }
[Start
    [A, EL=\leq 0.5
        [A1, EL=\leq 0.3,s sep+=-3mm
            [A11, EL=\leq 0.7]
            [A12, EL=\geq 0.7]
        ]
        %[,phantom,fit=band] % <---
        [A2, EL=\geq 0.3,s sep+=-3mm
            [A21, EL=\leq 0.7]
            [A22, EL=\geq 0.7]
         ]
    ]
    %[,phantom,fit=band]     % <---
    [B, EL=\geq 0.5
        [B1, EL=\leq 0.3,s sep+=-3mm
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
        %[,phantom,fit=band] % <---
        [B2, EL=\geq 0.3,s sep+=-3mm
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
    ]
]
    \end{forest}
\end{document}

enter image description here

And I had this on my machine before Zarko pinged me. (I didn't think it is worthwhile to add an update for it.)

\documentclass[border=3mm]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style={% Edge Label Style
      font=\footnotesize, inner sep=2pt,
      anchor=south #1, % label position: "ELS=west" or "EL=east"
      pos=0.6}
        }
\newcommand\EL{edge label}
    \begin{forest}
for tree={
    draw, semithick,
    parent anchor=children,
    s sep=4mm,
  l sep=13 mm,
    anchor=parent,% <--- work around alignment issue
        },
   EL/.style = {
   before typesetting nodes={%% <--- work around edge labels positioning
    where n=1{%
      edge label/.wrap value={node[ELS=east]{$#1$}}
    }{%
      edge label/.wrap value={node[ELS=west]{$#1$}}
    },
    where level=2{s sep+=-1mm}{s sep+=0.5mm}
                            }
                }
[Start
    [A, EL=\leq 0.5
        [A1, EL=\leq 0.3
            [A11, EL=\leq 0.7]
            [A12, EL=\geq 0.7]
        ]
        %[,phantom,fit=band] % <---
        [A2, EL=\geq 0.3
            [A21, EL=\leq 0.7]
            [A22, EL=\geq 0.7]
         ]
    ]
    %[,phantom,fit=band]     % <---
    [B, EL=\geq 0.5
        [B1, EL=\leq 0.3
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
        %[,phantom,fit=band] % <---
        [B2, EL=\geq 0.3
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
    ]
]
    \end{forest}
\end{document}

enter image description here

  • thank you very much for answer. apparently my question is not clear enough. i like to obtain in question showed image with removed ˙phantom` nodes (indicated by % <---). as i see, in your suggestion the distances between nodes at level 0 are equal. i like to have smaller between A11 and A12 and bigger between A21 and A22, etc. – Zarko Mar 28 at 20:39
  • @Zarko Sorry, didn't read carefully. Better now? – user121799 Mar 28 at 20:48
  • yes, that is. i need to check how you achieve this :-) – Zarko Mar 28 at 20:52
  • with help of your answer and after reading forest documentation again (and again) i figured out solution which i like to have: from your code i remove all s sep+=-3mm from nodes on the second level and to fortree add option where level=2{s sep+=-1mm}{}. it works fine. – Zarko Mar 28 at 23:18
  • @Zarko Yes, I have almost the same thing on my machine. where level=2{s sep+=...}{s sep+=...} but with other dimensions. – user121799 Mar 28 at 23:21
2

Just for record. With help of nice @marmot answer and after reading forest documentation again (and again) i figured out the solution which i looked for:

\documentclass[border=3mm, preview]{standalone}
\usepackage{forest}

\begin{document}
\tikzset{ELS/.style={% Edge Label Style
      font=\footnotesize\sffamily, inner sep=2pt,
      anchor=south #1, % label position: "ELS=west" or "ELS=east"
      pos=0.6}
        }
    \begin{forest}
for tree={
    draw,
    semithick,
%    fill=orange!20,
    parent anchor=children,
  l sep=13 mm,
  s sep=4mm,
    where level=2{s sep=1mm}{}
        },
   EL/.style = {
   before typesetting nodes={%
    where n=1{%
      edge label/.wrap value={node[ELS=east]{$#1$}}
    }{%
      edge label/.wrap value={node[ELS=west]{$#1$}}
    }
                            }
                }
[Start
    [A, EL=\leq 0.5
        [A1, EL=\leq 0.3
            [A11, EL=\leq 0.7]
            [A12, EL=\geq 0.7]
        ]
        [A2, EL=\geq 0.3
            [A21, EL=\leq 0.7]
            [A22, EL=\geq 0.7]
         ]
    ]
    [B, EL=\geq 0.5
        [B1, EL=\leq 0.3
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
        [B2, EL=\geq 0.3
            [B11, EL=\leq 0.7]
            [B12, EL=\geq 0.7]
        ]
    ]
]    \end{forest}
\end{document}

which gives desired result:

enter image description here

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.