TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
\documentclass[paper=a4, fontsize=10pt]{article} % A4
\usepackage{tikz}
\tikzstyle{every node}=[circle,draw]
\begin{document}

\begin{tikzpicture}[level/.style={sibling distance=50mm/#1}]
\node {6}
child {             
    child {
        node {2}  edge from parent node[left,draw=none] {0}
    }
    child {
        node {2}  edge from parent node[right,draw=none] {1}
    }
    node {2} edge from parent node[left,draw=none] {0}
}
child {
    node {$4$}
};
\end{tikzpicture}

\end{document}

What happens here is that the node (2) are edges inside the circle... for some reason. Is there a way to automatically label 0 for left and 1 for right?

Updated the code to :

\begin{forest}
                    for tree={
                        if n=1{edge label={node [midway, left, anchor=south] {0} } }{edge label={node [midway, right, anchor=south] {1} } },
                        draw,
                        circle,
                        if level=0{}{!u.s sep/.wrap pgfmath arg={#1}{35mm/(level())}},
                        anchor=mid,
                    }
                    [6
                        [2
                            [2
                                [1]
                                [1]
                            ]
                        ]
                        [4
                            [2]
                        ]
                    ]
                \end{forest}
share|improve this question
    
Please can you make your code compilable? You are making e.g. 0 a child node. But you want it, I think, to be an edge label. – cfr 8 hours ago
    
@cfr is this compilable? – Maxime Roussin-Bélanger 8 hours ago
    
Yes, should be ;). Although LaTeX will complain about all of french, paper=a4, fontsize=10pt not being valid options. – cfr 8 hours ago
up vote 3 down vote accepted

Forest is probably the best choice if you want to automate aspects of your tree-drawing e.g. automatically adding 0 labels on the left and 1 on the right.

For example:

\documentclass[tikz,multi,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    if n=1{edge label={node [midway, left, anchor=south] {0} } }{edge label={node [midway, right, anchor=south] {1} } },
    draw,
    circle,
    if level=0{}{!u.s sep/.wrap pgfmath arg={#1}{50mm/(level())}},
    anchor=mid,
  }
  [6
    [2
      [if]
      [(]
    ][4]
  ]
\end{forest}
\end{document}

Forest

The specification of trees is also very concise, as you can see.

share|improve this answer
    
Wow looks, very nice. For some reason I can't get the forest package? – Maxime Roussin-Bélanger 7 hours ago
    
It is a really neat package - very powerful, very flexible albeit a little slower than one might ideally prefer for larger, more complex trees. But, then, TikZ is slow anyway for larger, more complex things. – cfr 7 hours ago
    
I missed the fact that you said you can't get the package. Why not? It is included in the major TeX distributions i.e. TeX Live and MikTeX. You can also download it from CTAN. – cfr 7 hours ago
    
I do have MikTeX, so I'm wondering why it doesn't find it... from the logs 2016-04-12 21:20:58,851-0400 INFO pdflatex - installing package forest triggered by tex\latex\forest\forest.sty 2016-04-12 21:20:59,152-0400 FATAL pdflatex - The remote package repository is corrupted. You have to choose another repository. 2016-04-12 21:20:59,152-0400 FATAL pdflatex - Info: http://ctan.ijs.si/tex-archive/systems/win32/miktex/tm/packages/ 2016-04-12 21:20:59,152-0400 FATAL pdflatex - Source: Libraries\MiKTeX\PackageManager\PackageManager.cpp 2016-04-12 21:20:59,152-0400 FATAL pdflatex - Line: 2540 – Maxime Roussin-Bélanger 7 hours ago
    
That's a MikTeX problem, I think. Have you tried following the recommendation to choose another repository? I assume you do this in MikTeX's package manager, although I'm guessing really as I've never used it. – cfr 7 hours ago

Edit: After reading @cfr comments I'm not sure anymore if I understanding question correct. If the labels of the edges in the three had to be on the left, if the label is zero, and on the right if label is one, regardless if the nodes child is below left of parents or below right (that was my understanding), than this classic solution, not so net as with forest, also works :-):

\documentclass[tikz,border=3mm]{standalone}

\begin{document}
\begin{tikzpicture}[auto,
every node/.style={circle,draw},
     level/.style={sibling distance=50mm/#1}
                    ]
\def\lbl#1{\ifnum#1=0 edge from parent node[draw=none,swap] {0}
           \else      edge from parent node[draw=none]      {1}
           \fi} 
\node (1) {6} 
    child {node (2) {2}
        child {node {if}  \lbl{0}}
        child {node {(}   \lbl{1}}
          \lbl{0}}
child {node (3) {$4$}
     };
\end{tikzpicture}
    \end{document}

enter image description here

For label positioning I defined simple command \lbl (as shortness for "label"), and for the edge labels positioning use option auto. Also see the difference where is written node (2) and where edge between it and its parent. As you have in your MWE gives strange result.

share|improve this answer
    
This doesn't actually answer the question, does it? This puts the label left if it is 0 and right if it is 1, but, as I understood it, the question was how to put 0 if it is left and 1 if it is right i.e. to determine the content of the label according to the position rather than to determine the position according to the content. ;) – cfr 7 hours ago
    
Well, than I miss understand the question. I will delete it. – Zarko 7 hours ago
    
See what the OP says first! Maybe I misunderstood. Or maybe they'll prefer this. – cfr 7 hours ago
    
You are right. I only wrote new explanation, how I understand the question and I will add picture and some comments to OP MWE – Zarko 7 hours ago
    
Actually, it isn't also. If your interpretation is correct, my answer is not a solution at all... – cfr 7 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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