Take the 2-minute tour ×
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.

I have been struggling a bit with TikZ and how to build trees. What I need now is some advice on how to improve the node placement. Right now I have these 2 trees:

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows,trees,shapes,fit}
\begin{document}
\begin{tikzpicture}
[font=\small, edge from parent fork down, 
every node/.style={top color=white, bottom color=blue!25, 
rectangle,rounded corners, minimum size=6mm, draw=blue!75,
very thick, drop shadow, align=center},
edge from parent/.style={draw=blue!50,thick},
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm}, 
level 3/.style={sibling distance=2.5cm}, 
level distance=2cm,
]

\node {Paletizar} % root
    child { node {Iniciar} 
        child { node {Detectar Pacote}}
        child { node {Movimentar}}
        }
    child { node {Empacotar}
        child { [sibling distance=15mm] node {Posicionar}
            child { node {Pegar/Largar}}
            child { node {Organizar}}
            child { node {Movimentar}}
            }
        child { node {Agrupar}}
        child { node {Padronizar}}
        }
    child { node {Finalizar} 
        child { node {Detectar estado\\da palete}}
        child { node {Transmitir}}
};
\end{tikzpicture}


\vspace{2cm}

\begin{tikzpicture}
[font=\small, edge from parent fork down, 
    every node/.style={top color=white, bottom color=blue!25, 
    rectangle,rounded corners, minimum size=6mm, draw=blue!75,
    very thick, drop shadow, align=center},
    edge from parent/.style={draw=blue!50,thick},
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=3cm}, 
    level 3/.style={sibling distance=2cm}, 
    level distance=2cm,
    ]

    \node {Paletizador} % root
        child { node {Inicialização\\de sistema} 
            child { node {Sensor X}}
            child { node {Acção Y}}
            }
        child { node {Sacos}
            child { node {Linha/Coluna}
                child { node {Garra}}
            child { node {Rodar Garra}}
            child { node {Acção Y2}}
            }
        child { node {Euro Pallet}}
        child { node {Disposição $xyz$}
            child { node {$x=?$}}
            child { node {$y=?$}}
            child { node {$z=?$}}
            }
        }
    child { node {Sistema de despacho} 
        child { node {Sensor de peso\\/contador}}
        child { node {Tapete rolante\\de saída}}
};
\end{tikzpicture}

\end{document}

So basically what I want is a way to edit the code I have so that everything is perceptible which basically means that I need specific sibling distances to each child. I have tried to add a sibling distance in the child nodes but maybe it was in the wrong place or something else because it didn't change anything. I also tried to use below and all its derivations but the results were even worse because of the size of the boxes.

share|improve this question
    
Welcome to TeX.sx! It's not necessary to sign your questions (as there is already a box with your username below it) or to begin them with a greeting. –  Torbjørn T. Nov 20 '11 at 20:34

3 Answers 3

up vote 14 down vote accepted

Using tikz-qtree helps a bit with automatic spacing of siblings. It also provides a simpler input method. Here's your second tree done in that way:

\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[margin=1in]{geometry}
\usepackage{tikz-qtree}
\usetikzlibrary{shadows,trees}
\begin{document}
\tikzset{font=\small,
edge from parent fork down,
level distance=1.75cm,
every node/.style=
    {top color=white,
    bottom color=blue!25,
    rectangle,rounded corners,
    minimum height=8mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
    text depth = 0pt
    },
edge from parent/.style=
    {draw=blue!50,
    thick
    }}

\centering
\begin{tikzpicture}
\Tree [.Paletizador
        [.{Inicialização\\de sistema}
            [.{Sensor X} ]
            [.{Acção Y} ] ] 
        [.Sacos
            [.{Linha/Coluna} 
                [.{Garra} ]
                [.{Rodar Garra} ]
                [.{Acção Y2} ] ]
            [.{Euro Pallet} ]
            [.{Disposição $xyz$}
                [.{$x=?$} ]
                [.{$y=?$} ]
                [.{$z=?$} ] ] ] 
        [.{Sistema de despacho}
            [.{Sensor de peso\\/contador} ]
            [.{Tapete rolante\\de saída} ] ]
]
\end{tikzpicture}
\end{document}

output of code

share|improve this answer
    
tkiz-qtree helps a lot (I was actually learning how to use it now). However it does create a couple of blank spaces. Is there a way to compact it? Also I seem to loose some horizontal alignment, is there a way to fix that? Other than that it's perfect –  Duarte Silva Nov 20 '11 at 22:48
    
Do you mean the space around the "Linha/Coluna" node? That's determined by the width of its daughter nodes; I don't think there's an easy way to adjust that. As for the alignment of the "y=?" node, I have no idea why that is not aligned properly. –  Alan Munn Nov 20 '11 at 23:04
    
Ok, I'll leave it at that. Maybe when I have more time I'll try again. Thanks to everyone –  Duarte Silva Nov 20 '11 at 23:10
    
Thanks to @Jake, there's a solution to the horizontal alignment problem: I've added text depth=0pt to the node style parameters. –  Alan Munn Nov 21 '11 at 0:27

I am not completely sure that this is actually what you want. You can easily change your code a bit to get everything to fit though. Adding the fullpage package to ensure it fits on the page properly and slightly adjusting the sibling distances for the different levels is enough.

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usepackage{fullpage}
\usetikzlibrary{positioning,shadows,arrows,trees,shapes,fit}
\begin{document}
\begin{tikzpicture}
[font=\small, edge from parent fork down, 
every node/.style={top color=white, bottom color=blue!25, 
rectangle,rounded corners, minimum size=6mm, draw=blue!75,
very thick, drop shadow, align=center},
edge from parent/.style={draw=blue!50,thick},
level 1/.style={sibling distance=6cm},
level 2/.style={sibling distance=2.5cm}, 
level 3/.style={sibling distance=2.5cm}, 
level distance=2cm,
]
\node {Paletizar} % root
    child { node {Iniciar} 
        child { node {Detectar Pacote}}
        child { node {Movimentar}}
        }
    child { node {Empacotar}
        child { [sibling distance=15mm] node {Posicionar}
            child { node {Pegar/Largar}}
            child { node {Organizar}}
            child { node {Movimentar}}
            }
        child { node {Agrupar}}
        child { node {Padronizar}}
        }
    child { node {Finalizar} 
        child { node {Detectar estado\\da palete}}
        child { node {Transmitir}}
};
\end{tikzpicture}

\vspace{2cm}
\begin{tikzpicture}
[font=\small, edge from parent fork down, 
    every node/.style={top color=white, bottom color=blue!25, 
    rectangle,rounded corners, minimum size=6mm, draw=blue!75,
    very thick, drop shadow, align=center},
    edge from parent/.style={draw=blue!50,thick},
    level 1/.style={sibling distance=6cm},
    level 2/.style={sibling distance=2.5cm}, 
    level 3/.style={sibling distance=1.75cm}, 
    level distance=2cm,
    ]
    \node {Paletizador} % root
        child { node {Inicialização\\de sistema} 
            child { node {Sensor X}}
            child { node {Acção Y}}
            }
        child { node {Sacos}
            child { node {Linha/Coluna}
                child { node {Garra}}
            child { node {Rodar Garra}}
            child { node {Acção Y2}}
            }
        child { node {Euro Pallet}}
        child { node {Disposição $xyz$}
            child { node {$x=?$}}
            child { node {$y=?$}}
            child { node {$z=?$}}
            }
        }
    child { node {Sistema de despacho} 
        child { node {Sensor de peso\\/contador}}
        child { node {Tapete rolante\\de saída}}
};
\end{tikzpicture}
\end{document}

TikZ Tree diagram

You might want to look into some other package, like qtree to draw your trees, if the nodes don't need to look as fancy and you want some automatic placement. If you do want the fancyness of TikZ, there is also tikz-qtree.

share|improve this answer
1  
Since qtree is designed solely for linguistic trees, it has few options for changing the branch shape, so for these kinds of trees, tikz-qtree is a better choice. –  Alan Munn Nov 20 '11 at 21:16

A more compact tree can be specified very concisely using the newer and powerful forest. For example:

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{geometry}
\usepackage{forest}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{shadows}
\begin{document}
\tikzset{
  my node style/.style={
    font=\small,
    top color=white,
    bottom color=blue!25,
    rectangle,
    rounded corners,
    minimum size=6mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
  }
}
\forestset{
  my tree style/.style={
    for tree={
      my node style,
      parent anchor=south,
      child anchor=north,
      edge={draw=blue!50, thick},
      edge path={% |-| style branches from parent to child
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-5pt) -| (.child anchor)\forestoption{edge label};
      },
      if n children=3{% if a node has 3 children
        for children={% align the middle child with its parent
          if n=2{calign with current}{}
        }
      }{},
    }
  }
}
\centering

\begin{forest}
  my tree style
  [Paletizar
    [Iniciar
        [Detectar Pacote]
        [Movimentar]
        ]
    [Empacotar
        [Posicionar
            [Pegar/Largar]
            [Organizar]
            [Movimentar]
        ]
        [Agrupar]
        [Padronizar]
    ]
    [Finalizar
        [Detectar estado\\da palete]
        [Transmitir]
    ]
  ]
\end{forest}

\vspace{2cm}

\begin{forest}
  my tree style
  [Paletizador
        [Inicialização\\de sistema
            [Sensor X]
            [Acção Y]
            ]
      [Sacos
          [Linha/Coluna
            [Garra]
            [Rodar Garra]
            [Acção Y2]
          ]
        [Euro Pallet]
        [{Disposição $xyz$}
          [{$x=?$}]
          [{$y=?$}]
          [{$z=?$}]
        ]
      ]
    [Sistema de despacho
      [{Sensor de peso\\/contador}]
      [Tapete rolante\\de saída]
    ]
  ]
\end{forest}

\end{document}

compact trees

If you need to keep the trees in portrait and can adjust the relative alignments a little, you can do this quite easily. In this version, I also make use of the vertical space available in portrait orientation to increase the distance between levels a little. The first tree uses tier alignment using tier=<tier name> and the second uses empty nodes to adjust the alignment, setting all empty nodes to shape=coordinate by modifying the style definition.

\documentclass[a4paper,10pt]{article}
\usepackage{geometry}
\usepackage{forest}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{shadows}
\begin{document}
\tikzset{
  my node style/.style={
    font=\small,
    top color=white,
    bottom color=blue!25,
    rectangle,
    rounded corners,
    minimum size=6mm,
    draw=blue!75,
    very thick,
    drop shadow,
    align=center,
  }
}
\forestset{
  my tree style/.style={
    for tree={
      parent anchor=south,
      child anchor=north,
      l sep+=5pt,
      my node style,
      edge={draw=blue!50, thick},
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-7.5pt) -| (.child anchor)\forestoption{edge label};
      },
      if n children=3{
        for children={
          if n=2{calign with current}{}
        }
      }{},
      delay={if content={}{shape=coordinate}{}}
    }
  }
}
\centering

\begin{forest}
  my tree style
  [Paletizar
    [Iniciar
        [Detectar Pacote]
        [Movimentar]
        ]
    [Empacotar
        [Posicionar
            [Pegar/Largar]
            [Organizar]
            [Movimentar]
        ]
        [Agrupar]
        [Padronizar, tier=A]
    ]
    [Finalizar, tier=A
        [Detectar estado\\da palete]
        [Transmitir]
    ]
  ]
\end{forest}

\vspace{2cm}

\begin{forest}
  my tree style
  [Paletizador
        [Inicialização\\de sistema
            [Sensor X]
            [Acção Y]
            ]
      [Sacos
        [
          [
            [Linha/Coluna
              [Garra]
              [Rodar Garra]
              [Acção Y2]
            ]
            [Euro Pallet]
            [{Disposição $xyz$}
              [{$x=?$}]
              [{$y=?$}]
              [{$z=?$}]
            ]
          ]
        ]
      ]
    [Sistema de despacho
      [Sensor de peso\\/contador]
      [Tapete rolante\\de saída]
    ]
  ]
\end{forest}

\end{document}

portrait trees

share|improve this answer

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.