4

I have some problems I am not able to fix with this tree in TikZ:

  1. how can I avoid the empty space near the root?
  2. how can I write a text on branches? I used edge from parent but does not work how I expect.

This is the code I'm using:

 \begin{tikzpicture}
 [ level 1/.style={sibling distance=6em},
   level 2/.style={sibling distance=4em}, level distance=1cm,
   level 3/.style={sibling distance=2em}, level distance=1cm] 
\node (root) {}  [fill]  circle (1.5pt)
   child {  [fill]  circle (1.5pt) 
    child {  [fill]  circle (1.5pt)
        child {}
        child {}
    }
    child {
    }
}
child {  [fill]  circle (1.5pt) 
        child {}
        child {}
}
;
\node at (root)[right]{I};
\node at (root-1)[left] {II};
\node at (root-2)[right] {II};

\end{tikzpicture}    

And this is what I get: Fix this tree

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Adam Liter Dec 8 '14 at 16:12
  • What "empty space near the root" do you mean? – T. Verron Dec 8 '14 at 16:30
  • The space near the "I": I'd like branches to start from the dark circle, as they do where there is "II". – GiuliaB Dec 8 '14 at 16:36
6

In order to avoid the empty space at the root, you want to make the root a coordinate, rather than a node, since nodes have some minimal dimension, whereas a coordinate does not.

As for text on the branches, you can, in fact, do it with edge from parent. It's hard to say where you're going wrong, since there isn't an example in your MWE, but the MWE below includes an example where it's working.

You might want to look at page 326 of the TikZ and PGF documentation.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
    level 1/.style={sibling distance=6em},
    level 2/.style={sibling distance=4em}, level distance=1cm,
    level 3/.style={sibling distance=2em}, level distance=1cm
    ] 
\coordinate (root) {}  [fill]  circle (1.5pt)
    child {  [fill]  circle (1.5pt)
        child {  [fill]  circle (1.5pt)
            child {}
            child {}
            edge from parent
                node[left] {a}
        }
        child {
        }
    }
    child {  [fill]  circle (1.5pt) 
        child {}
        child {}
    }
    ;
\node at (root)[right]{I};
\node at (root-1)[left] {II};
\node at (root-2)[right] {II};
\end{tikzpicture} 
\end{document}

enter image description here

4

Because trees need a forest...

... and I like automated solutions...

\documentclass[tikz, border=5pt, mult, varwidth]{standalone}
\usepackage{forest}
\begin{document}
  \forestset{
    filled circle/.style={
      circle,
      text width=3pt,
      fill,
    },
    phantom/.append style={label={}},
    my label/.style n args=2{
      edge label={node [midway, #1, font=\scriptsize] {#2}}
    },
  }
  \begin{forest}
    for tree={
      inner sep=0pt,
      outer sep=0pt,
      tier/.wrap pgfmath arg={tier #1}{int(level)},
      where n=0{
        s sep=4em,
        label={right:\csname @Roman\endcsname{1}},
        for descendants={
          s sep=2em,
          l sep=10mm,
          where n=1{
            label/.wrap pgfmath arg={left:\csname @Roman\endcsname{#1}}{int(level()+1)}
          }{
            where n'=1{
              label/.wrap pgfmath arg={right:\csname @Roman\endcsname{#1}}{int(level()+1)}
            }{},
          },
        },
      }{},
    }
    [, filled circle
    [, filled circle, my label={left}{some text}
        [, filled circle
          []
          [, phantom]
        ]
        []
      ]
      [, filled circle, my label={right}{some text}
        []
        []
      ]
    ]
  \end{forest}
\end{document}

auto-labelled forest tree

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.