[Tex/LaTex] Drawing a tikz tree above a tikz-dependency tree with same leaves

linguisticstikz-pgftrees

I would like to achieve the following goal:

I have two different kind of trees that I would like to merge :

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{tikzpicture}[
    level/.style={sibling distance=2cm,
    level distance = 1cm}
]
    \tikzset{level 1/.style={sibling distance=4cm}}
    \node {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node {D} child{node {The}}
        }
        child[red] {
            node {\textcolor{red}{\textbf{N}}} child[black]{node {cat}}
        }
    }
child{
    node (vp) {\textcolor{blue}{\textbf{VP}}}
    child[blue] {
        node {\textcolor{blue}{\textbf{V}}} child[black]{node {sat}}
    }
    child[blue] {
        node {\textcolor{blue}{\textbf{PP}}}
        child {
            node {\textcolor{blue}{\textbf{P}}} child[black]{node {on}}
        }
        child[black] {
            node {NP}
            child {
                node {D} child{node {the}}
            }
            child {
                node {N} child{node {mat}}
            }
        }
    }
}
;
\end{tikzpicture}
\end{document}

Constituent tree

and a dependency tree :

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{dependency}
        \begin{deptext}[column sep=3em]
            \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
            \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
        \end{deptext}
        \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
        \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
        \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
        \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
        \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
    \end{dependency}
\end{document}

Dependency tree

So far I managed to get this, bu the tikz upper tree is not well-aligned (as it was) and I don't know how to achieve this.

\begin{dependency}
    \begin{deptext}[column sep=3em]
        \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
        \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
    \end{deptext}

    \node[yshift=7cm] {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node {D} child{node at ($(\wordref{1}{1})$) {}}
        }
        child[red] {
            node {\textcolor{red}{\textbf{N}}}
            child[black]{node at($(\wordref{1}{2})$) {}}
        }
    }
    child{
        node (vp) {\textcolor{blue}{\textbf{VP}}}
        child[blue] {
            node {\textcolor{blue}{\textbf{V}}}
            child[black]{node at($(\wordref{1}{3})$) {}}
        }
        child[blue] {
            node {\textcolor{blue}{\textbf{PP}}}
            child {
                node {\textcolor{blue}{\textbf{P}}}
                child[black]{node at($(\wordref{1}{4})$) {}}
            }
            child[black] {
                node {NP}
                child {
                    node {D} child{node at($(\wordref{1}{5})$) {}}
                }
                child {
                    node {N} child{node at($(\wordref{1}{6})$) {}}
                }
            }
        }
    }
    ;


    \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
    \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
    \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
    \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
    \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
\end{dependency}

Merged trees

If somebody can help me, it would be very nice,

Have a nice day.

Best Answer

This should get you started.

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{tikzpicture}[
    level/.style={sibling distance=2cm,
    level distance = 1cm}
]
    \tikzset{level 1/.style={sibling distance=4cm}}
    \node {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node (d1) {D}
        }
        child[red] {
            node (n1) {\textcolor{red}{\textbf{N}}}
        }
    }
child{
    node (vp) {\textcolor{blue}{\textbf{VP}}}
    child[blue] {
        node (v) {\textcolor{blue}{\textbf{V}}}
    }
    child[blue] {
        node {\textcolor{blue}{\textbf{PP}}}
        child {
            node (p) {\textcolor{blue}{\textbf{P}}}
        }
        child[black] {
            node {NP}
            child {
                node (d2) {D}
            }
            child {
                node (n2) {N}
            }
        }
    }
}
;
\begin{scope}[shift={(1cm,-2.2in)}]
\begin{deptext}[column sep=3em]
            \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
            \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
        \end{deptext}
        \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
        \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
        \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
        \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
        \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
\end{scope}
 \draw [->, very thick, red] (d1) -- (\wordref{1}{1});
 \draw [->, very thick, red] (n1) -- (\wordref{1}{2});
 \draw [->, very thick, red] (v) -- (\wordref{1}{3});
 \draw [->, very thick, red] (p) -- (\wordref{1}{4});
 \draw [->, very thick, red] (d2) -- (\wordref{1}{5});
 \draw [->, very thick, red] (n2) -- (\wordref{1}{6});
\end{tikzpicture}


\end{document}

enter image description here

I have removed the last children and added a label to the nodes and then using \wordref if dependency, draw the edge. Aligning them may be done manually.

Your code can be modified like this

\documentclass{article}
\usepackage{color,graphicx}
\usepackage{tikz}
\usepackage{tikz-dependency}
\usetikzlibrary{%
  shapes,%
  arrows,%
  positioning,%
  calc,%
  automata%
}
\definecolor{pf7}{RGB}{166, 118, 29}

\begin{document}
\begin{tikzpicture}[
    level/.style={sibling distance=2cm,
    level distance = 1cm},
    level 1/.style={sibling distance=4cm}
]
    \begin{scope}[shift={(1cm,0in)}]
    \begin{deptext}[column sep=3em]
        \textbf{The} \& \textbf{cat} \& \textbf{sat} \& \textbf{on} \& \textbf{the} \& \textbf{mat} \\
        \textbf{1}    \& \textbf{2}     \& \textbf{3} \& \textbf{4}    \& \textbf{5}     \& \textbf{6} \\
    \end{deptext}
    \end{scope}

    \node[yshift=5cm] {S}
    child{
        node (np) {\textcolor{red}{\textbf{NP}}}
        child {
            node {D} child{node at ($(\wordref{1}{1})$) {}}
        }
        child[red] {
            node {\textcolor{red}{\textbf{N}}}
            child[black]{node at($(\wordref{1}{2})$) {}}
        }
    }
    child{
        node (vp) {\textcolor{blue}{\textbf{VP}}}
        child[blue] {
            node {\textcolor{blue}{\textbf{V}}}
            child[black]{node at($(\wordref{1}{3})$) {}}
        }
        child[blue] {
            node {\textcolor{blue}{\textbf{PP}}}
            child {
                node {\textcolor{blue}{\textbf{P}}}
                child[black]{node at($(\wordref{1}{4})$) {}}
            }
            child[black] {
                node {NP}
                child {
                    node {D} child{node at($(\wordref{1}{5})$) {}}
                }
                child {
                    node {N} child{node at($(\wordref{1}{6})$) {}}
                }
            }
        }
    }
    ;


    \depedge[edge below]{3}{2}{\bf\textcolor{pf7}{nsubj}}
    \depedge[edge below]{4}{6}{\bf\textcolor{pf7}{pobj}}
    \depedge[edge below]{3}{4}{\bf\textcolor{pf7}{prep}}
    \depedge[edge below]{2}{1}{\bf\textcolor{pf7}{det}}
    \depedge[edge below]{6}{5}{\bf\textcolor{pf7}{det}}
\end{tikzpicture}
\end{document}

enter image description here