11

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?

hfingler
  • 641

2 Answers2

12
  • 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

hpesoj626
  • 17,282
  • I have to go. Just post your comment if this does not answer your concern. – hpesoj626 Jan 18 '13 at 06: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 01:07
  • Very nice and instructive answer! – Alan Munn Jan 19 '13 at 01:11
  • That is quite a complement coming from you @AlanMunn. Much appreciated. – hpesoj626 Jan 19 '13 at 01:14
  • I guess i can't get that output because of the tex version or something. I'll try to compile it in my pc instead of the cloud (sharelatex), maybe it'll work then. Thanks a lot. – hfingler Jan 19 '13 at 19:42
  • @hpesoj626 I really can't get the third output. Compiling using Sharelatex or TeXworks+TextMate give me the same output, a really nice tree but node from different subtrees won't be placed on the same column ({A, IDA} and {Meta-heurísticas} for example, they are placed just like the output from Code 2. Any idea why? – hfingler Jan 22 '13 at 04:42
  • Another question: why does the rightmost subtree tree get cut when I change the documentclass to "standalone"? The project this is in has all the figures in pdf format, they were all generated using the standalone class so there are no blanks around the figure, and then in the main .tex file all pdf's are included as figures. – hfingler Jan 22 '13 at 04:47
  • Here's the output i get with Code 3 but with standalone class: http://i.imgur.com/GqjZjeo.png – hfingler Jan 22 '13 at 04:49
  • Sorry, I have just read your message. When using standalone class, remove the geometry package. – hpesoj626 Jan 23 '13 at 01:31
  • Still can't get that output. Maybe it's the package version or something. Nodes within different subtrees won't be placed on a same column and even without the geometry package it outputs a clipped pdf. Thanks anyway, it's probably an issue on my side. – hfingler Feb 05 '13 at 01:59
  • If not clipped, your attached picture will look okay. try deleting your .aux file and compile again. – hpesoj626 Feb 05 '13 at 08:13
11

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