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.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI 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.
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}
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}