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 want to make a tree like this one:

original tree

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*} ]    
    ]  
    [.{Métodos aproximados} 
        [.{Algoritmos Heurísticos}
            [.{Meta-heurísticas}
                [{Meta-heurísticas baseadas em solução única} ]
                [{Meta-heurísticas baseadasem população} ]
            ]
            [{Heurísticas específicas do problema} ]
        ]
        [{Algoritmos de aproximação} ]    
    ] 
]
\end{tikzpicture}

}
\end{document}

Giving me this ugly WIDE tree:

enter image description here

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?

share|improve this question
    
Thanks for the edits Alan Munn, english is not my main language and I leaned a lot from them. –  polar Jan 22 '13 at 4:39
add comment

2 Answers

up vote 10 down vote accepted
  • 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*} ]    
    ]  
    [.{Métodos aproximados} 
        [.{Algoritmos Heurísticos}
            [.{Meta-heurísticas}
                [{Meta-heurísticas\\ baseadas em solução única} ]
                [{Meta-heurísticas\\ baseadasem população} ]
            ]
            [{Heurísticas específicas\\ do problema} ]
        ]
        [{Algoritmos\\ de aproximação} ]    
    ] 
]
\end{tikzpicture}

\end{document}

enter image description here

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*} ]    
    ]  
    [.{Métodos aproximados} 
        [.{Algoritmos Heurísticos}
            [.{Meta-heurísticas}
                [.{Meta-heurísticas\\ baseadas em solução única} ]
                [.{Meta-heurísticas\\ baseadasem população} ]
            ]
            [.{Heurísticas específicas\\ do problema} ]
        ]
        [.{Algoritmos\\ de aproximação} ]    
    ] 
]
\end{tikzpicture}

\end{document}

enter image description here

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*} ]    
    ]  
    [.{Métodos\\ aproximados} 
        [.{Algoritmos\\ Heurísticos}
            [.{Meta-heurísticas}
                [.{Meta-heurísticas\\ baseadas em\\ solução única} ]
                [.{Meta-heurísticas\\ baseadasem\\ população} ]
            ]
            [.{Heurísticas\\ específicas\\ do problema} ]
        ]
        [.{Algoritmos\\ de aproximação} ]    
    ] 
]
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
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 –  polar 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. –  polar 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. –  polar Jan 18 '13 at 20:23
    
@polar See my edit. –  hpesoj626 Jan 19 '13 at 1:07
show 9 more comments

Wide trees were precisely why I developed forest!

forest's rendering of the tree

\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*} ]    
    ]  
    [Métodos aproximados 
        [Algoritmos\\Heurísticos
            [Meta-heurísticas
                [Meta-heurísticas\\baseadas em solução única]
                [Meta-heurísticas\\baseadasem população]
            ]
            [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
share|improve this answer
    
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. –  polar 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
add comment

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.