2

I am trying to draw a tree overview and got an example from http://www.texample.net. But I have a problem with the vertical distance between the nodes (levels). Is there a way how tikz can automatically determine a distance so that there is no overlapping. Using e.g. "level distance=x.yem" for each node is a lot of work for large trees. Below a running example for a small part of the tree.

            \documentclass[paper=a4]{report}
            \usepackage[utf8]{inputenc} % UTF8 encoding

            \usepackage{pgfplots}
            \pgfplotsset{compat=newest}
            \pgfplotsset{plot coordinates/math parser=false}
            \usetikzlibrary{arrows,shapes,positioning,shadows,trees}

            \begin{document}



            \tikzset{
                basic/.style  = {draw, font=\sffamily, rectangle},
                root/.style   = {basic, rounded corners=6pt, thin, align=center,
                                                 fill=blue!60,text width=0.5\textwidth},
                level 2/.style = {basic, rounded corners=6pt, thin,align=center, fill=green!60,text width=0.3\textwidth},
                level 3/.style = {basic, rounded corners=2pt, thin, align=center, fill=pink!60,text width=0.2\textwidth},
            }

            \begin{tikzpicture}[level 1/.style={sibling distance=40mm},
                edge from parent/.style={->,draw},
                >=latex]

            % root of the the initial tree, level 1
            \node[root]
            [level distance=30mm]
            {Some loss calculation models}
            % The first level, as children of the initial tree
                child {node[level 2] (c1) {ALmann based models (analytical)}}
                child {node[level 2] (c2) {Loss separation stuff stuff}}
                child {node[level 2] (c3) {Mathematical models and others}};

            % The second level, relatively positioned nodes
            \begin{scope}[every node/.style={level 3}]
            \node [below of = c1, xshift=15pt] (c11) {Mathematic stuff with very long name};
            \node [below of = c11] (c12) {Mathematic2 other long name};
            \node [below of = c12] (c13){Mathematic3 short};
            \node [below of = c13] (c13){Mathematic4 meadium size name};

            \end{scope}

            % lines from each level 1 node to every one of its "children"
            \foreach \value in {1,2,3}
                \draw[->] (c1.195) |- (c1\value.west);

            \end{tikzpicture}
            \end{document}
  • 1
    This is developed into a huge library graphdrawing in the upcoming version 3 of TikZ. You can download the beta or CVS version from sourceforge and use it. – percusse Jan 28 '14 at 16:57
  • I just installed library. Unfortunately, LuaTeX is needed if I want to use the library (\usepgflibrary{graphdrawing}). Is there a way to use it with the standard latex compiler also? – Andy Sweden Jan 30 '14 at 16:45
  • I don't think so. but actually LuaTeX is recommended. is there any particular detail that disallow you using it? I'm almost always using it for a long time. there is no difference for standard usage plus yiunget to use the system fonts and other goodies. – percusse Jan 30 '14 at 17:07
  • OK, I will check LuaTeX if it is working with my thesis and packages I am using (e.g. pstricks, tabu, etc). Otherwise I have to go back to eps and pstricks I think. Thanks a lot anyway! – Andy Sweden Feb 5 '14 at 8:55
2

If you don't want to wait for TiKZ 3 and its graphdrawing library a valid option could be forest package. It automaticaly computes distances between nodes according their sizes.

Next code shows a starting point for a tree more or less similar to yours. You will need to read its manual to know how to adjust vertical subtree under its parent. You can also find help looking at previous questions in TX.SX about trees with and without forest.

\documentclass[tikz,border=3mm]{standalone}
\usepackage[utf8]{inputenc} % UTF8 encoding

\usepackage{forest}

\begin{document}
    \tikzset{basic/.style = {draw, font=\sffamily, rectangle},
            root/.style = {basic, rounded corners=6pt, thin,
                 align=center, fill=blue!60, 
                 text width=0.5\textwidth},
            level 2/.style = {basic, rounded corners=6pt, thin, 
                   align=center, fill=green!60,
                   text width=0.3\textwidth},
            level 3/.style = {basic, rounded corners=2pt, thin,
                   align=center, fill=pink!60,
                   text width=0.2\textwidth},
            }

\begin{forest}
% root of the the initial tree, level 1
[Some loss calculation models, root, for tree={level 2}
% The first level, as children of the initial tree
     [Almann based models (analytical), 
            for descendants={level 3, child anchor=west}, 
            for tree={ grow'=east }, calign=first
% The second level
        [Mathematic stuff with very long name]
        [Mathematic2 other long name]
        [Mathematic3 short]
        [Mathematic4 meadium size name]]
     [Loss separation stuff stuff]
     [Mathematical models and others]]
\end{forest}

\end{document}

enter image description here

  • I checked the forest package, but it seems it can just do straight lines between the children. Is there a way to draw lines like it is done in the example I posted first (having a central line connecting all children)? – Andy Sweden Jan 29 '14 at 15:57
  • @AndySweden: Unfortunately there is no example in forest documentation showing how to change edges, but here and here you have two examples. – Ignasi Jan 29 '14 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.