11

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.

8

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

  • @Corentin You are welcome :) – user11232 Mar 14 '15 at 14:57
12

Here's a forest approach I ought not have spent time playing with (even if it is Saturday!) ;).

What I like about this method is the automatic placement of the words and their alignment with their parent nodes. However, the drawing of the dependencies underneath is rather clunky and probably fragile.

Basically, when you get to the last node of the syntax tree, you say word={}{} where the first argument is the word and the second is a unique label. (This is necessary in case you have identical words in the same sentence.)

After the tree is complete, but before ending the forest, you can then use the pic depedge={}{}{}{} to draw the dependencies. This takes 4 arguments: the originating node, the terminating node, whether you want to be 1 layer down from the word or 2, and what the content of the label in the middle should be.

\documentclass[border=5pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{calc}
\begin{document}
  \newcounter{word}
  \forestset{
    colour group/.style={#1, edge=#1, for parent={#1}},
    word/.style n args=2{
      append={
          [{#1\\\stepcounter{word}\theword},
            name=#2,
            tier=word,
            font=\bfseries,
            align=center,
            inner ysep=1pt,
            for descendants={l=-2pt},
            [, name=#2-dep1, tier=dep1, dep node, edge path={\relax}
              [, name=#2-dep2, tier=dep2, dep node, edge path={\relax}
              ]
            ]
          ]
        },
    },
  }
  \tikzset{
    dep node/.style={shape=coordinate},
    dep type/.style={midway, fill=white, font=\scriptsize, inner sep=.75pt, text=orange!75!black, outer sep=0pt, draw, minimum height=8pt},
    pics/depedge/.style n args={4}{
      code={
        \ifnum#3=1
          \draw [->, rounded corners=2pt, pic actions] ({$(#1)!.075!(#2)$} |- #1.south) -- ($(#1-dep#3)!.125!(#2-dep#3)$) -- node [dep type] {#4} ($(#2-dep#3)!.125!(#1-dep#3)$) -- ({$(#2)!.075!(#1)$} |- #1.south);
        \else\ifnum#3=2
          \draw [->, rounded corners=2pt, pic actions] (#1.south) -- ($(#1-dep#3)!.075!(#2-dep#3)$) -- node [dep type] {#4} ($(#2-dep#3)!.075!(#1-dep#3)$) -- (#2.south);
        \fi\fi
      },
    },
  }

  \begin{forest}
    for tree={
      s sep+=10pt,
    }
    [S
      [NP
        [D, word={The}{the1}
        ]
        [N, colour group=red, word={cat}{cat}
        ]
      ]
      [VP
        [V, colour group=blue, word={sat}{sat}
        ]
        [PP, colour group=blue
          [P, colour group=blue, word={on}{on}
          ]
          [NP
            [D, word={the}{the2}
            ]
            [N, word={mat}{mat}
            ]
          ]
        ]
      ]
    ]
    \pic {depedge={cat}{the1}{1}{det}};
    \pic {depedge={mat}{the2}{1}{det}};
    \pic {depedge={sat}{cat}{1}{nsubj}};
    \pic {depedge={sat}{on}{1}{prep}};
    \pic {depedge={on}{mat}{2}{pobj}};
  \end{forest}
\end{document}

forest dependencies

  • @Corentin Thanks. I'm sure there must be a better way to add the dependency paths but I have no idea what at the moment! – cfr Mar 14 '15 at 23:52

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.