3

I'm trying to create the following tree with forest:

enter image description here

I'm getting this far:

\documentclass{article}
\usepackage{forest, amsmath}
\begin{document}
    \begin{forest}
        for tree = {%
            parent anchor = south,
            child anchor = north,
            s sep = 0em}
        [, phantom
            [, phantom
                [h, no edge, tier  = segment]
            ]
            [{[$-$R]}, calign = first
                [a, tier  = segment]
                [v, no edge, tier  = segment]
                [r, no edge, tier  = segment]
            ]
            [{[+R]}, calign = midpoint
                [, parent anchor = north
                    [$\stackrel{*}{\text{u}}$, tier  = segment]
                    [z, no edge, tier  = segment]
                    [u, tier  = segment]
                ]
                    [n, no edge, tier  = segment]
                    [u, tier  = segment]
                    [z, no edge, tier  = segment]
            ]
            [{[$-$R]}, calign = last
                [d, no edge, tier  = segment]
                [$\stackrel{*}{\text{a}}$, tier  = segment]
            ]           
        ]
    \end{forest}
\end{document}

enter image description here

It looks ok, but I have two things I'd like to improve:

  1. There's a big gap between the first segment h and the next segment a (in addition to there being too much space between all of the segments on the bottom tier, but I can live with that).
  2. The branching structure under [+R] should ideally have straight lines, so that the angle of the line doesn't change when there's a branching in the tree.

How can I achieve these two things?

  • On the second question google "nice empty nodes" on the site, to find various versions of solutions (each has their own application/weaknesses). And compilable code...? – Alan Munn Sep 17 '17 at 16:04
  • @AlanMunn Sorry, the first two lines disappeared in the copy-past routine. – Sverre Sep 17 '17 at 18:11
  • Your desiderata are in conflict. You can straighten the edges by spreading the children out, but then there will be large gaps between some of the children, where this is needed. Or you can have a constant spacing between the children, but then the lines can't be straight. – cfr Sep 17 '17 at 19:18
  • @AlanMunn nice empty nodes plays havoc here for me. Did you get it to work? – cfr Sep 17 '17 at 19:19
  • @cfr I hadn't tried anything when I made the comment, but you're right. Do you know why? I think it's because of the no edge nodes, but I haven't investigated. – Alan Munn Sep 17 '17 at 21:40
3

Your desiderata conflict. We can use calign=fixed edge angles to straighten the edges in the middle sub-tree. However, this will require spreading the children, which will increase the spaces between the letters in the final line.

straight edges with spaced children

Alternatively, we can keep the spacing of the terminal children more-or-less fixed, but then the edges cannot be straight.

cosy children with crooked edges

If you need both, you may have to fake it.

faking cosy children with straight edges

\documentclass[border=10pt]{standalone}
\usepackage[linguistics]{forest}
\usepackage{amsmath}
\begin{document}
\begin{forest}
  for tree={
    s sep'=0em,    
  },
  before typesetting nodes={
    where content={}{child anchor=center, parent anchor=center}{},
    where n children=0{tier=segment, inner sep=0pt}{},
  },
  [, phantom
    [{[$-$R]}
      [h, no edge]
      [a, calign with current edge]
      [v, no edge]
      [r, no edge]
    ]
    [{[+R]}, for tree={calign=fixed edge angles}
      [
        [$\stackrel{*}{\text{u}}$]
        [z, no edge]
        [u]
      ]
      [n, no edge]
      [u]
      [z, no edge]
    ]
    [{[$-$R]}
      [d, no edge]
      [$\stackrel{*}{\text{a}}$, calign with current edge]
    ]           
  ]
\end{forest}
\begin{forest}
  for tree={
    s sep'=0em,    
    inner sep=0pt,
  },
  before typesetting nodes={
    where content={}{coordinate}{},
    where n children=0{tier=segment}{},
  },
  [, phantom
    [{[$-$R]}
      [h, no edge]
      [a, calign with current edge]
      [v, no edge]
      [r, no edge]
    ]
    [{[+R]}
      [
        [$\stackrel{*}{\text{u}}$]
        [z, no edge]
        [u]
      ]
      [n, no edge]
      [u]
      [z, no edge]
    ]
    [{[$-$R]}
      [d, no edge]
      [$\stackrel{*}{\text{a}}$, calign with current edge]
    ]           
  ]
\end{forest}
\begin{forest}
  for tree={
    s sep'=0em,    
    inner sep=0pt,
  },
  before typesetting nodes={
    where content={}{coordinate}{},
    where n children=0{tier=segment}{},
  },
  [, phantom
    [{[$-$R]}
      [h, no edge]
      [a, calign with current edge]
      [v, no edge]
      [r, no edge]
    ]
    [{[+R]}, tikz+={
      \draw [\forestoption{edge}] (.parent anchor) -- (!11.child anchor) coordinate [midway] (p) (p) -- (!1l.child anchor);
    }
      [, for tree={no edge}
        [$\stackrel{*}{\text{u}}$]
        [z]
        [u]
      ]
      [n, no edge]
      [u]
      [z, no edge]
    ]
    [{[$-$R]}
      [d, no edge]
      [$\stackrel{*}{\text{a}}$, calign with current edge]
    ]           
  ]
\end{forest}
\end{document}
  • I'm not sure I agree that my desiderata conflict. There may be a conflict because of how forest does things, but when drawing this by hand, there are several ways to get what I want. One way is to draw the branches as if I first drew a branching structure from the first u to the third u, and then a line from the second u to the line from the first u. Another way is to draw the lower branches first, then continue the line up to the highest level, and then draw a line from there down to the third u. Is nothing like this possible with forest? – Sverre Sep 20 '17 at 13:42
  • @Sverre I meant they conflict in Forest terms. Clearly, they don't conflict per se, as my third example shows. But then you are drawing the edges in by hand and not by Forest, so to speak. The problem with your descriptions is that they ignore the fact that there is another node involved at the branching point. So it is not just a question of drawing the lines. You need that node to end up in such-and-such place. You could calculate that and move the node there. Then the edges would come out right. But you'd have to do that by hand, in that case, so it just adds a complication. – cfr Sep 20 '17 at 15:56
  • I'm trying to use this without the [linguistics] option (I'm just worried that if I put that option in my .sty file, I'll get it trouble when I need to do something that goes against what [linguistics] sets). Without [linguistics], however, the lines touch the top of the letters on the bottom tier. How do I prevent that? – Sverre Oct 9 '17 at 13:58
  • @Sverre Load the library separately and apply the defaults in a group, rather than passing it as a package option. If you load it, the defaults are available, but not applied. Then you can apply them just within local groups for the trees you want them to be applied to. – cfr Oct 9 '17 at 22:00
  • I didn't quite understand that. Would you mind adding a little code snippet at the end of your reply demonstrating what you just said? :) – Sverre Oct 13 '17 at 19:50

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.