# Text tree using tikz-qtree is too wide, don't know how to break lines and make the tree compact

I want to make a tree like this one:

It doesn't need to be exactly the same, but I want to translate the text, so I need to rebuild it, I'd also want to add a node, but that's easy.

My first try:

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{pgfplotstable}
\usepackage{graphicx}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage{tikz-qtree}
\begin{document}

\begin{tikzpicture}
\Tree
[.{Métodos de otimização}
[.{Métodos exatos}
[.{Branch-and-X}
[{Branch-and-bound} ]
[{Branch-and-cut} ]
[{Branch-and-price} ]
]
[{Programação de restrições} ]
[{Programação dinâmica} ]
[{A*, IDA*} ]
]
[.{Algoritmos Heurísticos}
[.{Meta-heurísticas}
[{Meta-heurísticas baseadas em solução única} ]
]
[{Heurísticas específicas do problema} ]
]
[{Algoritmos de aproximação} ]
]
]
\end{tikzpicture}

}
\end{document}


Giving me this ugly WIDE tree:

The first problem is that I don't know how to break lines in each node. I tried \\\\ and it does nothing; should I use a tabular for each text line?

The second problem is the subnodes of each subtree won't be placed on the same column. Is this fixable or hackable? Like "Metaheuristics" on the fourth row and "A*, IDA*" on the third, they are from different subtrees, but are on the same column.

Any other ideas?

-
Thanks for the edits Alan Munn, english is not my main language and I leaned a lot from them. –  hfingler Jan 22 '13 at 4:39

• To use line breaks you can use every tree node key and use center alignment.

\tikzset{every tree node/.style={align=center}}

• You can shorten the sibling distance to make it more compact.

\tikzset{sibling distance=6pt}

• You can also set the level distance

\tikzset{level distance=60pt}


With these applied, you have:

## Code 1

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

%\usepackage{tikz}
\usepackage{tikz-qtree}

\tikzset{level distance=60pt,
sibling distance=6pt,
every tree node/.style={align=center},
}

\begin{document}

\begin{tikzpicture}
\Tree
[.{Métodos de otimização}
[.{Métodos exatos}
[.{Branch-and-X}
[{Branch-and-bound} ]
[{Branch-and-cut} ]
[{Branch-and-price} ]
]
[{Programação\\ de restrições} ]
[{Programação\\ dinâmica} ]
[{A*, IDA*} ]
]
[.{Algoritmos Heurísticos}
[.{Meta-heurísticas}
[{Meta-heurísticas\\ baseadas em solução única} ]
]
[{Heurísticas específicas\\ do problema} ]
]
[{Algoritmos\\ de aproximação} ]
]
]
\end{tikzpicture}

\end{document}


I don't know though if you are after the following instead.

## Code 2

\documentclass{standalone}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

%\usepackage{tikz}
\usepackage{tikz-qtree}

\tikzset{level distance=60pt,
sibling distance=6pt,
every tree node/.style={align=center},
}

\begin{document}

\begin{tikzpicture}
\Tree
[.{Métodos de otimização}
[.{Métodos exatos}
[.{Branch-and-X}
[.{Branch-and-bound} ]
[.{Branch-and-cut} ]
[.{Branch-and-price} ]
]
[.{Programação\\ de restrições} ]
[.{Programação\\ dinâmica} ]
[.{A*, IDA*} ]
]
[.{Algoritmos Heurísticos}
[.{Meta-heurísticas}
[.{Meta-heurísticas\\ baseadas em solução única} ]
]
[.{Heurísticas específicas\\ do problema} ]
]
[.{Algoritmos\\ de aproximação} ]
]
]
\end{tikzpicture}

\end{document}


## Edit

You can also shorten the first level sibling distance with a negative distance like

\tikzset{level 1/.style={sibling distance=-100pt}}


You can also adjust each level sibling distance this way.

To make your tree look like the picture you posted, you can use the arrows library to style your edges. (I don't know if doing this is a sin but here it is anyway.) It is a sin in the pgf world to scale the text with the figure but this seems to be the default behavior in tikz-qtree. I scaled down to 0.8 so that it fits inside a portrait a4paper. Here is another full code.

## Code 3

\documentclass[10pt]{article}
\usepackage{geometry}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

%\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{arrows}

\tikzset{level distance=60pt,
sibling distance=0pt,
level 1/.style={sibling distance=-100pt},
level 2/.style={sibling distance=0pt},
level 3/.style={sibling distance=0pt},
execute at begin node=\strut,
every tree node/.style={align=center},
edge from parent/.append style={very thick,-stealth}
}
\begin{document}

\begin{tikzpicture}[scale=0.8]
\Tree
[.{Métodos de otimização}
[.{Métodos exatos}
[.{Branch-and-X}
[.{Branch-\\ and-\\ bound} ]
[.{Branch-\\ and-\\ cut} ]
[.{Branch-\\ and-\\ price} ]
]
[.{Programação\\ de restrições} ]
[.{Programação\\ dinâmica} ]
[.{A*, IDA*} ]
]
[.{Algoritmos\\ Heurísticos}
[.{Meta-heurísticas}
[.{Meta-heurísticas\\ baseadas em\\ solução única} ]
]
[.{Heurísticas\\ específicas\\ do problema} ]
]
[.{Algoritmos\\ de aproximação} ]
]
]
\end{tikzpicture}

\end{document}


-
I have to go. Just post your comment if this does not answer your concern. –  hpesoj626 Jan 18 '13 at 6:19
Nope, this solves the problem. The third tree is awesome, thanks for the answer. :D –  hfingler Jan 18 '13 at 19:44
I can't get the output from your edit. Could you just show the \tikzset for that tree? Ive tried using 2 tikzset, or insert the first sibling into the Code 2 tikzset, but i never get that output. Thanks. –  hfingler Jan 18 '13 at 20:12
It's like this line: \tikzset{level 1/.style={sibling distance=50pt}} doesn't do anything, i tried putting in high and low values and the output doesn't change. –  hfingler Jan 18 '13 at 20:23
@polar See my edit. –  hpesoj626 Jan 19 '13 at 1:07

Wide trees were precisely why I developed forest!

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{forest}

\begin{document}
\tikzset{>=latex}

\begin{forest} for tree={align=center,edge=->,}
[Métodos de otimização
[Métodos exatos
[Branch-and-X,for children={l+=2ex}
[Branch-\\and-bound]
[Branch-\\and-cut]
[Branch-\\and-price]
]
[Programação\\de restrições]
[Programação\\dinâmica]
[{A*, IDA*} ]
]
[Algoritmos\\Heurísticos
[Meta-heurísticas
]
[Heurísticas específicas\\ do problema]
]
[Algoritmos\\de aproximação]
]
]
\end{forest}
\end{document}


Since there's a lot of text, it had to be helped a bit:

• manual line breaks: these are enabled by align=center;
• l+ pushes a node down
-
Very nice package! +1 –  hpesoj626 Jan 29 '13 at 23:28
Good one. Googled the package and it wasn't that easy to download the sty itself. Found it and compiled on my mac, I'll end up using this tree. THanks a lot. –  hfingler Feb 5 '13 at 2:00
@polar deal: i'll be uploading the .sty in new versions. –  Sašo Živanović Feb 5 '13 at 21:20