1

I would like to add a rectangle upon a subtree like in the following picture. How to do that ?

enter image description here

Here is the forest code.

\documentclass{article}
\usepackage{forest}
\useforestlibrary{linguistics}

\begin{document}
\pagestyle{empty}

\begin{forest}
    for tree = {
        sn edges,
        grow'=0,
        l=2.5cm,
        s sep=0.2cm,
        anchor=west,
        child anchor=west}
  [
    [O
      [R
        [T]
      ]
      [T
        [R]
      ]
    ]
    [R
      [O
        [T]
      ]
      [T
        [O]
      ]
    ]
    [T
      [O
        [R]
      ]
      [R
        [O]
      ]
    ]
  ]
\end{forest}

\end{document}

1 Answer 1

5

You can assign names to nodes in forest, so you can use this together with the Tikz fit library to draw a node around the nodes you have chosen.

If you don't want to use a node, you can also just do (doesn't require any library):

\draw[red,thick] (bottomright.south east) rectangle (left.west|-topright.north);

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{forest}
\useforestlibrary{linguistics}
\usetikzlibrary{fit}

\begin{document}
\begin{forest}
    for tree = {
        sn edges,
        grow'=0,
        l=2.5cm,
        s sep=0.2cm,
        anchor=west,
        child anchor=west}
  [
    [O,name=left
      [R
        [T,name=topright]
      ]
      [T
        [R,name=bottomright]
      ]
    ]
    [R
      [O
        [T]
      ]
      [T
        [O]
      ]
    ]
    [T
      [O
        [R]
      ]
      [R
        [O]
      ]
    ]
  ]
\node[draw=red,thick,fit=(left)(topright)(bottomright)] {};
\end{forest}
\end{document}
16
  • That's great and easy to use ! Thanks for this.
    – projetmbc
    Sep 13, 2016 at 22:13
  • @projetmbc I added an alternative, see edit. :)
    – Alenanno
    Sep 13, 2016 at 22:14
  • 2
    If the aim is to enclose all children of a node, you can also apply tikz={\node [draw,red,fit to=tree] {};} directly to that node.
    – Dai Bowen
    Sep 13, 2016 at 22:25
  • 4
    @projetmbc Or , tikz+={\node [draw=red, fit=() (!u) (!uu)] {};} to circle the node, the node's parent and the node's grandparent, for example.
    – cfr
    Sep 13, 2016 at 23:10
  • 2
    @LukasCB Use labels, like label={above:<test>} in the node options.
    – Alenanno
    Sep 15, 2016 at 19:19

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.