8

Update:

ref comments: I've slightly corrected the terminology and the title. I am using the term diagram now, instead of tree, and I am just asking on how to draw this diagram in Tikz. Any tool or library would be OK. I used qtree since that is what I know now, any other method is welcome.

end of update

I'd like help/hint on how to draw this diagram

enter image description here

The part I am having problem with is how to make it go from B C children to one node D then fork out again.

This is latest attempt. I tried also to use grow'=up in the middle, but could not figure the syntax without splitting it into two pictures. I wanted it all to be one picture.

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}

\begin{document}
\tikzset{font=\small,
         edge from parent path={(\tikzparentnode.south) -- +(0,-8pt) -| (\tikzchildnode)}}

\begin{tikzpicture}
    \Tree [.A
             [.B ]
             [.C ]
          ]
\begin{scope}[xshift=0in,yshift=-1.5cm]
    \Tree [.D
             [.E ]
             [.F ]
          ]
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

update 7/21/13 1 AM

Here is a general diagram as requested. But I am not really asking someone to draw all of this for me in Tikz, I was just asking for the technique on how to join then fork in a middle of building a general diagram since all the examples I've seen seem to show forks at each node.

enter image description here

6
  • 2
    Technically that's not a tree anymore, so I'm not hopeful the library provides for this. Jul 21, 2013 at 4:35
  • 2
    I agree here with @SeanAllred, at least for the library part. Is this the only tree you want to draw? How much un-tree-y would your diagrams get? In this simple case (i.e. one tree down, one tree up where the last generation connects to one node) you could use the link tree style from my answer to How to reflect duplicating a tree horizontally? Jul 21, 2013 at 4:44
  • @Qrrbrbirlbel This is an example. The actual tree can and will contain many parts in it that can fork/join like this in many places. So looking for a general solution I can use in a more general tree constructions. thanks. ps. will look at your link tree style, it seems like it might be what I wanted.
    – Nasser
    Jul 21, 2013 at 5:04
  • That’s what I feared. Could you add images of one or two of the “trees” you want to draw? Depending on the complexity and the treeiness I can see solutions using tikz-qtree (or any tree-growing packages/libraries), chains, purely the positioning library and a little manual labor or maybe even a matrix/tikz-cd with manually drawn lines. Jul 21, 2013 at 5:30
  • @Qrrbrbirlbel I put the example with the idea that if I know how to do that one diagram, then I can incorporate the idea in any diagram I want to make later on, since I know I would need to do this all the time. I do not have a specific diagram at this moment to show, but will update the above and add few additional parts to it. I wanted to keep the example simple. I was not really looking for someone to draw the whole diagrams for me, but only to learn how to do the fork/join in the middle of a diagram. I looked at forest package also, but need to learn it more... thanks
    – Nasser
    Jul 21, 2013 at 6:09

2 Answers 2

6

In addition to the solution suggested by @Qrrbrbirlbel in the comments, playing around a little bit with \usetikzlibrary{positioning}, defining some \node's in the trees that you had already provided, and using \coordinate allows you to get the output that you want.

It might not be the most elegant way to do so, but as has already been pointed out in the comments, this is not really one tree.

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{positioning}

\begin{document}

\tikzset{font=\small,
         edge from parent path={(\tikzparentnode.south) -- +(0,-8pt) -| (\tikzchildnode)}}

\begin{tikzpicture}
    \Tree [.A
             [.\node(B){B}; ]
             [.\node(C){C}; ]
          ]
\begin{scope}[xshift=0in,yshift=-2cm]
    \Tree [.\node(D){D};
             [.E ]
             [.F ]
          ]
\end{scope}

\coordinate [above=.2cm of D] (D') {};
\draw[-] (B) |- (D');
\draw[-] (C) |- (D');
\draw[-] (D') -- (D);
\end{tikzpicture}

\end{document}

enter image description here

1
  • Both your answer and Qrrbrbirlbel answer are really great answers. Thanks both for your help and time.
    – Nasser
    Jul 22, 2013 at 6:37
8

Here is an idea for the second, bigger diagram.

It uses (unfortunately) only TikZ. I have used my paths.ortho library for the |-| path (which has been improved with the from center option for the hvvh family in combination with the ratio option) and my positioning-plus library.

The latter is used for two different/improved usages of the below key.

As the children’s position are determined in relation to the parent and their anchors (and not like the positioning library’s below=of that uses the border), we can mimic this behavior by using the on grid option of positioning. The positioning-plus library checks for a factor delimited by : before the of part. This makes it possible to place a node double the usual node distance apart from another node. This is used for the placement of the E node (in relation to A). This will fail for the L node as this is not centered below E but below G and H. Thus below=of (E-2)(E-3) can be used (this works by creating a pseudo node around E-2 and E-3 similar to the fit key).

The connection is done by an edge style that is kind of reversed to the edge from parent path that is used with child automatically. The parents style helps us to connect many parent-children.

The text depth has been set to zero due to Q’s depth.

References

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning-plus,paths.ortho}
\tikzset{
  parents/.style 2 args={
     @parents/.style={insert path={edge[edge from children path] (#1-##1)}},
     @parents/.list={#2}},
  edge from parent path={
    (\tikzparentnode\tikzparentanchor) |-| (\tikzchildnode\tikzchildanchor)},
  edge from children path/.style={
    to path={
      (\tikztostart\tikzchildanchor) |-| (\tikztotarget\tikzparentanchor) \tikztonodes}}
}
\begin{document}
\begin{tikzpicture}[
  node distance=\tikzleveldistance and \tikzsiblingdistance,
  on grid,
  text depth=+0pt,
  hvvh=from center
  ]

\node (A) {A}
  child {node {B}}
  child {node {C}};

\node[below=2:of A] (E) {E} [parents={A}{1,2}]
  [sibling distance/.expanded=2*\the\tikzsiblingdistance]
  child {node {F}
    child {coordinate
      [anchor=center,sibling distance/.expanded=.5*\the\tikzsiblingdistance]
      child {node {N}}
      child {node {Q}}
    }
    child {node {R}}
  }
  child {node {G}}
  child {node {H}};

\node[below=of (E-2)(E-3)] (L) {L} [parents={E}{2,3}]
  child {node {M}}
  child {node {N}}
  child {node {K}}
;
\end{tikzpicture}
\end{document}

Output

enter image description here

1
  • Will your ortho library be incorporated into TexLive distribution at sometime in the future? thanks
    – Nasser
    Jul 21, 2013 at 22:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .