如何在一棵树上形成多个根?

如何在一棵树上形成多个根?

我正在尝试制作一棵这样的树,但有多个根,即按照我拥有的树,在 n 级创建多个节点,而不仅仅是节点“S”。

\begin{tikzpicture}
\Tree 
[.\node (level0-right) {S}; 
  [.NP 
    [.Det \node (level3-left) {the}; 
    ] 
    [.N cat 
    ] 
  ]
  [.\node (level1-right) {VP}; 
    [.V sat 
    ]
    [.\node (level2-right) {PP}; 
      [.P on 
      ]
      [.\node (level3-right) {NP}; 
        [.Det the 
        ] 
        [.\node (level4-right) {N}; \node (level5-right) {mat}; 
        ] 
      ]
    ] 
  ] 
]
\foreach \Value/\Text in {0/{level $n$},1/{level $n-1$},2/{level $n-2$},3/{$\vdots$},4/{level $2$},5/{level $1$}}
{  
  \node[anchor=west] 
    at ([xshift=1cm]{level5-right}|-{level\Value-right}) 
    {\Text};
}
\end{tikzpicture}

我该如何实现这一点?谢谢您的帮助。

答案1

我从未使用过tikz-qtree(与 相对qtree),但这是 Forest 中的想法。Forest 比 更强大、更灵活tikz-qtree,但学习曲线可能更陡峭。

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}
  before typesetting nodes={
    tempcounta/.max={level}{tree},
    !*\forestregister{tempcounta}l.alias=final,
  },
  for tree={fit=band},
  [, phantom
    [S
      [NP
        [Det [the]
        ]
        [N [cat]
        ]
      ]
      [VP
        [V [sat]
        ]
        [PP
          [P [on]
          ]
          [NP
            [Det [the]
            ]
            [N [mat]
            ]
          ]
        ]
      ]
    ]
    [S
      [NP
        [Det [the]
        ]
        [N [cat]
        ]
      ]
      [VP
        [V [sat]
        ]
        [PP
          [P [on]
          ]
          [NP
            [Det [the]
            ]
            [N [mat]
            ]
          ]
        ]
      ]
    ]
  ]
  \coordinate (e) at (current bounding box.east);
  \def\tempa{}%
  \foreach \i in {level $1$,level $2$,$\quad\quad\vdots$}
  {
    \node [anchor=mid west] at (!{name=final\tempa}.mid -| e) {\i};
    \expandafter\gdef\expandafter\tempa\expandafter{\tempa,u}
  }
  \foreach \i [evaluate=\i as \j using {(\i==1) ? "" : int(-\i+1)}] in {1,2,3}
  {
    \node [anchor=mid west] at (!*\i l.mid -| e) {level $n\j$};
  }
\end{forest}
\end{document}

或者,额外的 TiZ 代码可以在树的序言中指定:

\begin{forest}
  before typesetting nodes={
    tempcounta/.max={level}{tree},
    !*\forestregister{tempcounta}l.alias=final,
    tikz+={
      \coordinate (e) at (current bounding box.east);
      \def\tempa{}%
      \foreach \i in {level $1$,level $2$,$\quad\quad\vdots$}
      {
        \node [anchor=mid west] at (!{name=final\tempa}.mid -| e) {\i};
        \expandafter\gdef\expandafter\tempa\expandafter{\tempa,u}
      }
      \foreach \i [evaluate=\i as \j using {(\i==1) ? "" : int(-\i+1)}] in {1,2,3}
      {
        \node [anchor=mid west] at (!*\i l.mid -| e) {level $n\j$};
      }
    },
  },
  for tree={fit=band},
  [, phantom
    [S
      [NP
        [Det [the]
        ]
        [N [cat]
        ]
      ]
      [VP
        [V [sat]
        ]
        [PP
          [P [on]
          ]
          [NP
            [Det [the]
            ]
            [N [mat]
            ]
          ]
        ]
      ]
    ]
    [S
      [NP
        [Det [the]
        ]
        [N [cat]
        ]
      ]
      [VP
        [V [sat]
        ]
        [PP
          [P [on]
          ]
          [NP
            [Det [the]
            ]
            [N [mat]
            ]
          ]
        ]
      ]
    ]
  ]
\end{forest}

无论哪种方式,结果都是一棵具有不可见根的树,因此它似乎有多个根:

森林版

相关内容