5

I appreciate it if you let me know the most elegant way to draw a crossed hierarchy such as the followings:

    X
   /\
  Y  Z
  /\/\
 p q  t

q has two parents Y and Z.

7

This is a hack because it uses the fact that the distance between nodes doesn't change from level to level and so by default branches will overlap:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node {X} 
   child {
     node {Y}
     child {
        node {p}
     }
     child {
        node {q}
     }
   }
   child {
      node {Z}      
      child {
         node {\phantom{q}}      
      }
      child {
         node {t}      
      }
   }
;
\end{tikzpicture}
\end{document}

sample output

The edge from Z to q is not really to the q but to an invisible q that sits on top of the first q.

A better way to do it is to name the nodes and draw the extra edge after the tree. This way if the q node moves the extra edge will still point to it. You can create a missing child node to keep the spacing as if there were an actual first child.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
   level 1/.style={sibling distance=20mm},
   level 2/.style={sibling distance=15mm}
]
\node {X} 
   child {
     node {Y}
     child {
        node {p}
     }
     child {
        node (q) {q}
     }
   }
   child {
      node (Z) {Z}      
      child[missing] {
         node {q}
      }
      child {
         node {t}      
      }
   }
;
\draw (Z) -- (q);
\end{tikzpicture}
\end{document}

sample output

  • @MasoodGhayoomi If this really helped you, apart from upvoting it, please accept this answer by clicking on the check mark. This marks your problem as solved. :) – hpesoj626 Mar 19 '13 at 1:07

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.