4

I would like to add the labels "H" and "L" to the X and Y connectors like those included below the Z nodes. Here is my code so far:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
  arn_x/.style = {treenode, circle, white, draw=black,fill=red,
     text width=1.5em, very thick},
  }

\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1.5cm}] 
\node [arn_x] {X}
    child{ node [arn_x] {Y}      
                child{ node [arn_x] {Z} 
                child{ node  {LLL} edge from parent node[above left]
                         {$L$}} 
                            child{ node  {LLH} edge from parent node[above right]
                        {$H$}   }
            }
            child{ node [arn_x] {Z}
                                child{ node  {LHL} edge from parent node[above left]
                                {$L$}}                      
                            child{ node  {LHH} edge from parent node[above right]
                                {$H$}   }
            }                            
    }
     child{ node [arn_x] {Y} 
        child{ node [arn_x] {Z} 
            child{ node  {HLL} edge from parent node[above left]
                {$L$}} 
            child{ node  {HLH} edge from parent node[above right]
                {$H$}   }
        }
        child{ node [arn_x] {Z}
            child{ node  {HHL} edge from parent node[above left]
                {$L$}}                      
            child{ node  {HHH} edge from parent node[above right]
                {$H$}   }
        }                            
    }
; 
\end{tikzpicture}
\end{document}
  • Welcome to TeX.SE! – marmot Sep 8 '18 at 4:38
  • You could do this automatically with Forest, if that's an option. – cfr Sep 13 '18 at 4:49
4

Yes, this is a bit confusing. You need to add the nodes rather "late". See the description in the pgfmanual right before section 21.3.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
  arn_x/.style = {treenode, circle, white, draw=black,fill=red,
     text width=1.5em, very thick},
  }

\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1.5cm}] 
\node [arn_x] {X}
    child{ node [arn_x] {Y}      
                child{ node [arn_x] {Z} 
                child{ node  {LLL} edge from parent node[above left]
                         {$L$}} 
                            child{ node  {LLH} edge from parent node[above right]
                        {$H$}   } edge from parent node[above left] {$L$}
            } 
            child{ node [arn_x] {Z}
                                child{ node  {LHL} edge from parent node[above left]
                                {$L$}}                      
                            child{ node  {LHH} edge from parent node[above right]
                                {$H$}   } edge from parent node[above right] {$H$}
            } edge from parent node[above left] {$L$}                            
    }
     child{ node [arn_x] {Y} 
        child{ node [arn_x] {Z} 
            child{ node  {HLL} edge from parent node[above left]
                {$L$}} 
            child{ node  {HLH} edge from parent node[above right]
                {$H$}   } edge from parent node[above left] {$L$}
        } 
        child{ node [arn_x] {Z}
            child{ node  {HHL} edge from parent node[above left]
                {$L$}}                      
            child{ node  {HHH} edge from parent node[above right]
                {$H$}   } edge from parent node[above right] {$H$}
        } edge from parent node[above right] {$H$}                           
    }
; 
\end{tikzpicture}
\end{document}

enter image description here

  • Perfect! The extra bit of info pointing me to the relevant section in the manual was really helpful, much appreciated. – alleyway Sep 8 '18 at 5:17

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.