TransWikia.com

How can label an edge of a tree which was manually added?

TeX - LaTeX Asked by ang on December 11, 2020

One can use TikZ to create a tree manually , or use the forest package to make the process easier. Below I show how I can use the forest package to create a binary tree, and turn it into a recombinant tree by using TikZ to draw some edges manually. While I can add a label to any edge which wasn’t manually added (as I do below with the label Kangaroo), I cannot figure out how to add a label to an edge which was manually added. For example, how do I label the edge from the node $S_1(T)=4$ to the node $S_2(TH)=S_2(HT)=8$ (see the following picture)?

recombinant binary tree

I simply tried copy-pasting the same command (i.e. edge label={node [near end, above left, font=footnotesize]{Kangaroo}}) in the code below after (T) -- (TH), but it didn’t work.

begin{forest}  
for tree={grow'=east}
[{$S_0=8$ },
    [{$S_1(T)=4$ },name=T,  
        [{$S_2(TT)=2$}, edge label={node [near end, above  left, font=footnotesize]{Kangaroo}} ]
        [, phantom]
    ]
    [, phantom
        [{qquad $S_2(TH)=S_2(HT)=8$},name=TH  ] 
    ]
    [{$S_1(H)=16$}, name=H
        [, phantom]
        [{$S_2(HH)=32$} ] 
    ]   
] 
draw (T) -- (TH)  (H) -- (TH);
end{forest}

Fyi, the [,phantom] nodes are just empty nodes to help with spacing, they are not really necessary.

One Answer

You are looking for l sep dimension

Also the sloped version of label with positioning pos=0.5 or midway adjusted with the help of yshift

enter image description here

begin{forest} 
    for tree={grow'=east, s sep=2em, l sep=2cm}
    [{$S_0=8$ },
    [{$S_1(T)=4$ },name=T,  
    [{$S_2(TT)=2$}, edge label={node [pos=0.5, above, yshift=-4pt ,sloped, 
     font=scriptsize]{Kangaroo}} ]
    [, phantom]
    ]
    [, phantom
    [{qquad $S_2(TH)=S_2(HT)=8$},name=TH  ] 
    ]
    [{$S_1(H)=16$}, name=H
    [, phantom]
    [{$S_2(HH)=32$} ] 
    ]   
    ] 
    draw (T) -- (TH)  (H) -- (TH);
end{forest}

EDIT

enter image description here

Add the following --

node at ($(T)!0.5!(TH)$)
        [rotate=-22, yshift=5pt]
        {footnotesizecolor{red} Kangaroo};

MWE

 begin{forest} 
    for tree={grow'=east, s sep=2em, l sep=2cm}
    [{$S_0=8$ },
    [{$S_1(T)=4$ },name=T,  
    [{$S_2(TT)=2$}, edge label={node [pos=0.5,above,yshift=-4pt,sloped, 
    font=footnotesize]{Kangaroo}} ]
    [, phantom]
    ]
    [, phantom
    [{qquad $S_2(TH)=S_2(HT)=8$},name=TH  ] 
    ]
    [{$S_1(H)=16$}, name=H
    [, phantom]
    [{$S_2(HH)=32$} ] 
    ]   
    ] 
    draw (T) -- (TH)  (H) -- (TH);
    node at ($(T)!0.5!(TH)$)
    [rotate=-22, yshift=5pt]
    {footnotesizecolor{red} Kangaroo};
end{forest}

Correct answer by js bibra on December 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP