5

I have a diagram in tikz-qtree like figure A, but I'd like something like figure B. Is it possible, to alter the properties of only one branch? (these figures were made with Dia and arrows are not important)

edit: (added my modified code, based on Alan Munn's answer)

\begin{center}
\begin{tikzpicture}[grow'=right,level distance=1.45in,sibling distance=.2in]

\tikzset{edge from parent/.style= 
{thick, draw, edge from parent fork right},
  every tree node/.style=
    {draw,minimum width=1.3in,text width=1.15in,align=center}
}
\Tree 
  [.parent
    [.child0
      grandchild0-0
      grandchild0-1
      [[.{grandchild0-2} 
        {greatgrandchild0} 
        {greatgrandchild0}
        {greatgrandchild0} 
        {greatgrandchild0}
      ]] 
      grandchild0-3
    ] 
    [.child1
      [.grandchild1-0 ] 
      [.grandchild1-1 ] 
    ] 
  ]
\end{tikzpicture}
\end{center}
3
  • 4
    It would be helpful to post the TikZ code that you have already in a complete compilable document.
    – Alan Munn
    May 8, 2012 at 18:44
  • @Greg Your second edit is a perfectly acceptable solution (especially for a one-off diagram) so instead of editing your question you should post it as a solution. It's fine to post answers to your own questions (and even accept them (although there's a 2 day waiting period for that, I think).
    – Alan Munn
    May 8, 2012 at 21:29
  • Yeah, I edited because I couldn't post own topic. Now, I've just I done. It was a solution to that issue. The funny thing is, atfer 2 days requirements have changed, so finally I will rahter use qtree. :D Thanks for your help. :)
    – Greg
    May 11, 2012 at 15:55

3 Answers 3

7

You can add extra brackets around a node to extend its branch:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees} % this is to allow the fork right path

\begin{document}

\begin{tikzpicture}[level distance=1.25in,sibling distance=.15in,scale=.75]
\tikzset{edge from parent/.style= 
            {thick, draw,
                edge from parent fork right},every tree node/.style={draw,minimum width=1in,text width=1in, align=center},grow'=right}
\Tree 
    [. parent 
        [.{nice child0}
                [.{grandchild0-0 } ]
            [.{grandchild0-1 } ]
            [.{grandchild0-2 } ]
            [.{grandchild0-3 } ]
        ]
        [[[.child1
            [.{grandchild1-0 } ]
            [.{grandchild1-1 } ]
            [.{grandchild1-2 } ]
        ]]]
        [.child2 ]
        [.child3 ]
    ]
\end{tikzpicture}
\end{document}

output of code

2
  • Thank you, but there is another thing I'd like to modify on it. Namely to reduce its height. Using your figure nodenames... the gap between "nice child0" and "child2" is as wide as child1's grandchild. But I'd like to reduce it. This is why I mainly wanted the modification you gave me.
    – Greg
    May 8, 2012 at 19:25
  • @greg tikz-qtree is designed to automatically calculate the node spacing (which is why it's so useful), but this comes at the expense of not having individual control of specific nodes, I think.
    – Alan Munn
    May 8, 2012 at 19:44
3

Here's a solution using forest which is an extremely flexible and powerful package for drawing trees. It offers the automatisation of qtree plus the ability to customise the format and placement of individual nodes, branches and edges, when required.

It does an especially good job of automatically packing the nodes to make compact trees. [But you could further compress the tree with manual adjustments if required.]

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    grow'=0,
    parent anchor=east,
    child anchor=west,
    anchor=west,
    thick,
    draw,
    edge path={
      \noexpand\path [draw, thick, \forestoption{edge}] (!u.parent anchor) -- +(5pt,0) |- (.child anchor)\forestoption{edge label};
    },
    if level=1{
      tier=children,
      if n=2{
        for children={
          tier=grandchildren 2,
          delay={
            edge path={
              \noexpand\path [draw, thick, \forestoption{edge}] (!u.parent anchor) -| ([xshift=-5pt].child anchor) -- (.child anchor)\forestoption{edge label};
            },
          }
        },
      }{
        for children={tier=grandchildren 1}
      },
    }{},
  }
  [root
    [child 1
      [grandchild 1
        [,phantom, tier=grandchildren 2]
      ]
      [grandchild 2]
      [grandchild 3]
    ]
    [child 2
      [grandchild 4]
      [grandchild 5]
      [grandchild 6]
    ]
    [child 3
      [grandchild 7]
      [grandchild 8]
    ]
  ]
\end{forest}
\end{document}

special second child

Note that if you just want the grandchildren packed as shown in your answer, the solution is significantly simpler:

\begin{forest}
  for tree={
    grow'=0,
    parent anchor=east,
    child anchor=west,
    anchor=west,
    thick,
    draw,
    edge path={
      \noexpand\path [draw, thick, \forestoption{edge}] (!u.parent anchor) -- +(5pt,0) |- (.child anchor)\forestoption{edge label};
    },
  }
  [root
    [child 1
      [grandchild 1]
      [grandchild 2]
      [grandchild 3]
      [grandchild 4]
    ]
    [child 2]
    [child 3]
    [child 4
      [grandchild 7]
      [grandchild 8]
    ]
  ]
\end{forest}

simple packed nodes

1
  • Would be so nice if you could explain what you did on the first solution.
    – Jan
    Sep 16, 2017 at 8:25
1

I managed to find a solution to my problem without qtree based on this one: How to modify the distance between branches when drawing trees using TiKZ?

The code:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{trees}%szögletességhöz kell
\begin{document}

\begin{tikzpicture}[grow'=right,level distance=90pt,sibling distance=30pt]

\tikzset{edge from parent/.style= 
{thick, draw, edge from parent fork right}
}

    \node [draw] (z){parent}
        child { 
            node[draw] (a) {child0}
            child { 
                node[draw] (c) {grandchild0-0} 
            }
            child { 
                node[draw] (d) {grandchild0-1} 
            }   
            child { 
                node[draw] (e) {grandchild0-2} 
            }   
            child { 
                node[draw] (f) {grandchild0-3} 
            }   
        }
        child {
            node[draw] (b) {child1}
        }
        child {
            node[draw] (b) {child2}
        }
        child {
            node[draw] (b) {child3}
            child { 
                node[draw] (c) {grandchild3-0} 
            }
            child { 
                node[draw] (c) {grandchild3-1} 
            }
        }
        ;

    \path (z) -- (a);
    \path (z) -- (b);
    \path (a) -- (c);
    \end{tikzpicture}

\end{document}

output

1
  • 2
    I realise it is your question, but I don't see how this answers it!
    – cfr
    Apr 5, 2015 at 13:22

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.