1

I'm trying to make a tree like this, but with multiple roots, ie, following the tree that I have, making multiple nodes at level n instead of having only the node '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}

How do I make that possible? Thanks for helping.

  • Please complete your code so we can compile it. What you describe is not a tree but a more generic graph. However, you can fake it, if you wish, using a phantom root. Or you can draw multiple trees. Which solution is best depends a bit on why you are tempted to call it a single 'tree'. – cfr Dec 18 '16 at 21:31
2

I've never used tikz-qtree (as opposed to qtree), but here is the idea in Forest. Forest is more powerful and flexible than tikz-qtree, but probably has a steeper learning curve.

\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}

Alternatively, the additional TikZ code may be specified in the tree's preamble:

\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}

Either way, the result is a tree with an invisible root, which therefore appears to have multiple roots:

Forest version

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.