Text FormattingFitting the length of each branch for this tree

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
215
Posts: 1
Joined: Tue Aug 29, 2017 7:01 pm

Fitting the length of each branch for this tree

Post by 215 »

I found this tree example from the LaTeX Cookbook in

http://www.texample.net/tikz/examples/tree/

Which i've used to build my own tree, but i have some problems with the length of the branch being the samme for all of them, thereby making some of the leaves overlap..

as seen here:

Code: Select all

% A simple Tree
% Author: Stefan Kottwitz
\documentclass[border=10pt]{standalone} 
%%%<
\usepackage{verbatim}
%%%>
\begin{comment}
:Title: A simple Tree
:Tags: Trees;Cookbook
:Author: Stefan Kottwitz
:Slug: tree

A simple tree with a style for all nodes.
\end{comment}

\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[sibling distance=15em,
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20}]]
  \node {Sentence}
    child { node {W1} 
        child { node {M1} 
            child { node {P1}}
            child { node {P2}}}
        child { node {M2} 
            child { node {P1}}
            child { node {P2}}}}
    child { node {W2} 
        child { node {M1}
            child { node {P1} }}}
    child { node {W3} 
        child { node {M1} }
        child { node {M2} }};
    
    \end{tikzpicture}
\end{document}
Creating this image.
tree.png
tree.png (19.92 KiB) Viewed 2448 times
How do fix the overlapping leaves, or how do set the length for the branches individually...
Last edited by Stefan Kottwitz on Wed Aug 30, 2017 1:20 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Fitting the length of each branch for this tree

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

You can adjust the sibling distance for each level individually, such as:

Code: Select all

\documentclass[border=10pt]{standalone} 
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[%sibling distance=15em,
    level 1/.style={sibling distance=8em},
    level 2/.style={sibling distance=6em},
    level 3/.style={sibling distance=3em},
  every node/.style = {shape=rectangle, rounded corners,
    draw, align=center,
    top color=white, bottom color=blue!20}]]
  \node {Sentence}
    child { node {W1} 
        child { node {M1} 
            child { node {P1}}
            child { node {P2}}}
        child { node {M2} 
            child { node {P1}}
            child { node {P2}}}}
    child { node {W2} 
        child { node {M1}
            child { node {P1} }}}
    child { node {W3} 
        child { node {M1} }
        child { node {M2} }};
    \end{tikzpicture}
\end{document}
tree-adjusted.png
tree-adjusted.png (15.57 KiB) Viewed 2447 times
LaTeX.org admin
Post Reply