如何在 LaTeX 中创建句法树并对齐它们?

如何在 LaTeX 中创建句法树并对齐它们?

我想要创建句法树并在 LaTeX 中对齐它们,如下图所示。

在此处输入图片描述

你能帮助我吗?

答案1

你会需要

  • tikz 包
  • tikz-qtree 包

这段代码将使您非常接近您想要实现的目标:

\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usepackage{tikz-qtree}
\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}
\newcommand{\subscript}[1]{\ensuremath{_{\textrm{#1}}}}
\begin{document}


\tikzset{level 1/.style={level distance=20pt}}
\tikzset{level 2/.style={level distance=30pt}}
\tikzset{level 3+/.style={level distance=20pt}}
\tikzset{frontier/.style={distance from root=10\baselineskip}}
\begin{tikzpicture}[remember picture]
\Tree
[.{ST}
    [.{NP}
        [.NNP \node[](john){John}; ]
        [.NNP \node[](smith){Smith}; ] ]%done
    [.{VP} % level 2 VP contains VP1 CC VP2
       [.{VP\subscript{1}} 
        [.{VBD} \node[](walked){walked}; ]
        [.{NP}
            [.{PRP\$} \node[](his){his}; ]
            [.{NN} \node[](dog){dog}; ] ] ]
        [.{CC} \node[](and){and}; ] 
        [.{VP\subscript{2}} 
            [.ADVP 
                [.{RB} \node[] (afterwards) {afterwards}; ] ]
            [.{VBD} \node[] (met) {Met}; ]
            [.{NP} 
                [.{NNP} \node[] (mary) {Mary}; ] ] ] ]
    [.{.}
        [[[\node[](dot){.}; ] ] ] ] ] ]

\begin{scope}[yshift=-9.5cm,xshift=-3cm,grow'=up]
\Tree
[.{ST}
    [.{NP} 
        [.{NNP} \node[] (john2) {John}; ]
        [.{NNP} \node[] (smith2) {Smith}; ]]
    [.{VP} 
        [.{VBD} \node[] (walked2) {walked}; ]
        [.{NP} 
            [.{PRP\$} \node[] (his2) {his}; ]
            [.{NN} \node[] (dog2) {dog}; ] ]]
    [.{.}
        [[[[\node[] (dot2){.}; ]]]] ] ]
\end{scope}

\begin{scope}[yshift=-9.5cm,xshift=3cm,grow'=up]
\Tree
[.{ST}
    [.{NP}
        [.{PRP} \node[] (he2) {He}; ] ]
    [.{VP}
        [.{VBD} \node[] (met2) {met}; ]
        [.{NP}
            [.{NNP} \node[] (mary2) {Mary}; ] ]
        [.{ADVP}
            [.{RB} \node[] (later2) {later}; ] ] ]
        [.{.}
            [[[[{.} ]]]] ] ]
\end{scope}
        %draw the lines
\begin{scope}[remember picture, overlay]
    \draw[black,dotted,thick](john)     to (john2);
    \draw[black,dotted,thick] (smith)   to (smith2);
    \draw[black,dotted,thick] (walked)to (walked2);
    \draw[black,dotted,thick] (his)     to (his2);
    \draw[black,dotted,thick] (dog)     to (dog2);
    \draw[black,dotted,thick] (dot)     to [out=220,in=50] (dot2);
    \draw[black,dotted,thick] (met)     to (met2);
    \draw[black,dotted,thick] (mary)  to (mary2);
\end{scope}
\end{tikzpicture}


\end{document}


\end{document}

一旦我对齐了叶节点,我就会发布更新的代码!我不确定为什么叶子.没有与其余叶子对齐。

更新.使叶节点与其他叶子对齐的问题的解决方案是将其嵌套在括号 [[[]]] 内,这使其处于更深的层次。


示例输出

示例输出已更新以匹配代码

如果您想了解更多信息,请查看Alan Munn 的详细回答请点击此处

相关内容