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 2377 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

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
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 2376 times
LaTeX.org admin
Post Reply