4

How can I connect the very first edges in the following examples ?

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}


% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]


\begin{tikzpicture}[grow=right, sloped]
\node {}
    child {
        node{T}        
            child {
                node[label=right: T] {}
            }
            child {
                node[label=right: O] {}
            }
    }
    child {
        node{O}        
            child {
                node[label=right: T] {}
            }
            child {
                node[label=right: O] {}
            }
    };
\end{tikzpicture}

\end{document}
  • Note that \tikzstyle is deprecated. – cfr Sep 12 '16 at 15:39
2

It's not clear to me why you are using labels instead of simply using the node contents themselves. This makes the tree overly complicated. But you can connect the edges of the root node by adjusting the edge from parent path.

Also, the standard TikZ markup for trees is quite verbose and complicated. You'll find it faster to use a dedicated tree package such as forest to do this. I've shown your tree in both modes in the example below. I've also changed the deprecated \tikzstyle syntax to the current syntax.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\usepackage{forest}
\useforestlibrary{linguistics}
\begin{document}
\pagestyle{empty}


% Set the overall layout of the tree
\tikzset{
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every tree node/.style={anchor=base},
level 1/.style={level distance=3.5cm, sibling distance=3.5cm},
level 2/.style={level distance=3.5cm, sibling distance=2cm}
}


\begin{tikzpicture}[grow=right]
\node[] {}
    child {
        node{T}        
            child {
                node {T}
            }
            child {
                node {O}
            }
    }
    child {
        node{O}        
            child {
                node {T}
            }
            child {
                node {O}
            }
    };
\end{tikzpicture}

\begin{forest} for tree={sn edges,grow'=0,l=3.5cm,s sep=1cm,anchor=west,child anchor=west}
[ [O [O] [T ] ] [T [O ] [T ]]]
\end{forest}

\end{document}

output of code

  • Thanks for the TikZ solution and you are right, forest is a better choice here. Maybe I will try to add some shapes upon my tree, and if I do that, TikZ should help me... maybe. – projetmbc Sep 12 '16 at 20:49
  • @projetmbc forest includes all of the power of TikZ since it is written using it. So anything you want to do to nodes or edges with TikZ can be done much more simply with forest. Take a look a the forest questions on the site (and also other tikz-trees questions, which often have forest solutions. – Alan Munn Sep 12 '16 at 21:20
  • It's good news ! – projetmbc Sep 13 '16 at 14:46

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.