5

I am trying to display a classification tree on papers in my work with the forest package for LaTeX. So far, I have the following code:

\begin{forest}                      
for tree={grow=0, s sep=0pt, 
edge= thick,  
anchor=base west,  
font=\strut\footnotesize\sffamily},
                                %
[,phantom                           % Paranormal root  (BOO!)
                                %  
[2E-VRP                               % Real branch   
[One,tier=b, 
    [Yes, tier=c]
    [No, tier=c]
    ]
[Two,tier=b, 
    [Yes, tier=c]
    [No, tier=c,
        [{ex1 ex2 ex3}, tier=d]]
    ]
]                          
                                %
[T1,for tree={color=gray,no edge}
[T2, tier=b % Ethereal branch
[T3, tier=c,
[T4, tier=d]]]]                  
                                %  
]                                   % Closing poltergeist
                                %
\end{forest}

resulting in this figure:

none

How can I get ex1, ex2, and ex3 below each other, that is, on separate lines?

2 Answers 2

6

You need to add align=left to for tree options and then write ex1\\ ex2\\ ex3. With some small further simplifications your MWE can be:

Edit:

Considering your wish from comment below:

\documentclass[border=3.141592mm]{standalone}
\usepackage{forest}
\tikzset{
LBL/.style = {font=\footnotesize\sffamily, text=gray, above=5mm}
        }

\begin{document}
    \begin{forest}
for tree={
    align=left,
    font=\footnotesize\sffamily,
    anchor=west,
% tree style
    grow=0, 
    edge= thick,
    l sep=4mm,
    s sep=4mm,
        },
[2E-VRP, name=l1
    [One,
        [Yes]
        [No]
    ]
    [Two, name=l2
        [Yes]
        [No, name=l3
            [ex1\\ ex2\\ ex3, name=l4]
        ]
    ]
]
\path   (l1 |- l4) node[LBL] {T1}
        (l2 |- l4) node[LBL] {T2}
        (l3 |- l4) node[LBL] {T3}
        (l4)       node[LBL] {T4};
\end{forest}
\end{document}

enter image description here

Addendum: In the case, that you like to have a mirrored diagram, then you need to replace grow=0˙ with ˙grow'=0 and move nodes name accordingly (that level label will stay at top of the diagram):

\documentclass[border=3.141592mm]{standalone}
\usepackage{forest}
\tikzset{
LBL/.style = {font=\footnotesize\sffamily, text=gray, above=5mm}
        }

\begin{document}
    \begin{forest}
for tree={
    align=left,
    font=\footnotesize\sffamily,
    anchor=west,
% tree style
    grow'=0,
    edge= thick,
    l sep=4mm,
    s sep=4mm,
        },
[2E-VRP, name=l1
    [One, name=l2
        [Yes, name=l3]
        [No]
    ]
    [Two
        [Yes]
        [No
            [ex1\\ ex2\\ ex3, name=l4]
        ]
    ]
]
\path   (l1 |- l3) node[LBL] {T1}
        (l2 |- l3) node[LBL] {T2}
        (l3)       node[LBL] {T3}
        (l4 |- l3) node[LBL] {T4};
\end{forest}
\end{document}

enter image description here

8
  • This is what I also considered, but I thought that it may destroy vertical alignment. Seemingly without the curly brackets it does not. Nice! Commented Mar 24, 2021 at 13:25
  • 1
    @JasperHabicht, thank you for edit! Curly braces are needed only when in node text are commas, so I drop them :-). +1 for your answer!
    – Zarko
    Commented Mar 24, 2021 at 13:35
  • @StudentNL, is above comment for me? If you have question forJasper, you should write comment below his answer ;-). Now the levels label are aligned to left of tree nodes (by anchor=west). If you like to change this, you need to write them on different way -- out of tree. I will add addendum to my answer ASAP.
    – Zarko
    Commented Mar 24, 2021 at 15:39
  • @Zarko you were right ;). My apologies. My main remaining question concerns the alignment of T1 and 2E-VRP (most obvious here).
    – Student NL
    Commented Mar 24, 2021 at 15:43
  • @Zarko thank you for the suggestion, you helped me a lot. Final question: how can I turn of the alphabetic ordering that is currently taking place? As you can read in the source code, I first define One, then Two, but it is given in opposite order in the tree.
    – Student NL
    Commented Mar 24, 2021 at 17:55
4

You could add line breaks and add an alignment using [{ex1 \\ ex2 \\ ex3}, tier=d, align=left]. But I guess, this would require some additional vertical shift.

A cleaner approach would probably be to add ex1, ex2 and ex3 as children of the parent node, remove the edges of ex1 and e3 and shift them a bit closer together:

\documentclass{article}

\usepackage{forest}

\begin{document}
            
\begin{forest}                      
for tree={grow=0, s sep=0pt, 
edge= thick,  
anchor=base west,  
font=\strut\footnotesize\sffamily},
                                %
[,phantom                           % Paranormal root  (BOO!)
                                %  
[2E-VRP                             % Real branch   
[One,tier=b, 
    [Yes, tier=c]
    [No, tier=c]
    ]
[Two,tier=b, 
    [Yes, tier=c]
    [No, tier=c, s sep=-10pt
        [ex3, tier=d, no edge ]
        [ex2, tier=d ]
        [ex1, tier=d, no edge ] ]
    ]
]                          
                                %
[T1, for tree={color=gray, no edge}
[T2, tier=b % Ethereal branch
[T3, tier=c,
[T4, tier=d]]]]                  
                                %  
]                                   % Closing poltergeist
                                %
\end{forest}    

\end{document}

enter image description here

You must log in to answer this question.

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