1

I have a tree preamble that generates trees with fixed angles, empty nodes, and centered middle nodes in ternary branching environments. I'd like to modify it to always give empty nodes vertical edges, such that nested empty nodes appear to have a continuous vertical edge.

In the MWE below, I achieve the desired effect with the existing preamble by manually adding an invisible third sibling with [,phantom] on the other side of the empty node, which is fine for such small trees but makes typing out larger trees cumbersome. Is there an efficient way to set the angle of empty nodes to 0 by default, or if not, to automatically add the [,phantom] in these environments? Despite some fiddling around, I wasn't able to figure it out. Thanks!

\documentclass{article}

\usepackage[linguistics]{forest}

\begin{document}
    \begin{forest} 
        for tree={
            calign=fixed edge angles, 
            calign angle=60, 
            delay={for children={
                if n=2{if n'=2{%
                before computing xy={s/.average={s}{siblings}}
                }{}}{}}
                }, % middle node is centered in ternary branching 
        }, 
        delay={
            where content={}{ % just avoiding shape=coordinate here for empty nodes
                shape=rectangle,
                for current and siblings={anchor=north},
                parent anchor=north,
                minimum size=0.001pt,
                inner sep=0.001pt,
                }{}
            }
%           
    [a
        [b]
        [
            [,phantom] % don't want this
            [
                [d]
                [e
                    [f]
                    [g]
                    [h]
                ]
            ]
            [c]
        ]
        [,phantom] % or this
    ]   
\end{forest}
\end{document}
1
  • When I try to compile your code, I get an error: ! Dimension too large. <recently read> \pgf@x . Can you fix it?
    – cfr
    Commented May 10 at 18:33

1 Answer 1

1

Your original code gives an error as it exceeds the maximum dimension TeX permits during calculations, so I've substituted a style I've used on this site before (somewhere!) called pretty nice empty nodes, but I've modified it to accept an argument. If specified, the contents of the argument should be keys to be applied to the empty nodes, in addition to those applied by the style.

I've also switched the handling of middle children at ternary branches. I'm guessing what you want here is alignment and it is better, in that case, to ask directly for alignment as we can do that before the nodes are packed. While this makes no difference in the current tree, it produces a better result if, say, we have fellowship of the ring rather than f. In that case, the averaging code will cause overlapping nodes, whereas the alternative align middle of three defined below will not. Whether this matters in your case, I don't know, but I'd prefer to provide more robust code when possible.

We can actually use the same trick for both align middle of three and the empty nodes. In both cases, we apply calign with current edge. We don't need to specify the angle or insert additional phantom nodes. We just leave it to forest's packing mechanism to sort things out.

\documentclass[tikz,border=5pt]{standalone}
% \documentclass{article}
\usepackage[linguistics]{forest}
% ateb: https://tex.stackexchange.com/a/717677/ i gwestiwn argo: https://tex.stackexchange.com/q/717592/
\forestset{%
  pretty nice empty nodes/.style={%
    for tree={%
      calign=fixed edge angles,
      parent anchor=children,
      delay={%
        if content={}{%
          inner sep=0pt,
          edge path'={(!u.parent anchor) -- (.children)},
          #1,
        }{},
      },
    },
  },
  align middle of three/.style={%
    before typesetting nodes={%
      if={ > On= On= & {n}{2} {n'}{2} }  
      {%
        calign with current edge,
      }{},
    },
  },
}
\begin{document}
\begin{forest} 
  for tree={
    calign angle=60, 
    align middle of three,% middle node is centered in ternary branching 
  },
  pretty nice empty nodes={
    for current and siblings={anchor=parent},
    parent anchor=children, 
    calign with current edge,
  },
  [a
    [b]
    [
      [
        [d]
        [e
          [f]
          [g]
          [h]
        ]
      ]
      [c]
    ]
  ]   
\end{forest}
\end{document}

My images are pretty crap right now, but here's an attempt:

aligned empty nodes and middle children

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .