1

In the following tree, I would like the bottom right node (in the base tier) to be directly under its parent, but it seems to automatically align with its sibling in the tier above. I've tried several of the calign options, but I don't want to change the positioning of the parent, only the positioning of the bottom child.

Is there a way to adjust the horizontal positioning of only this child?

\documentclass{beamer}
\usepackage{forest}

\begin{document}
\begin{frame}
  \begin{forest}
    for tree = {
        draw, circle,
        s sep = 6mm
      }
      [
        [ [[,tier=base]] [] ]
        [ [] [,tier=base] [] ]
      ]
  \end{forest}
\end{frame}
\end{document}

binary tree

1 Answer 1

5

The horizontal position of the child (or more precisely, its relative position wrt the parent in the dimension, perpendicular to the grotwh direction of the tree) can be adjusted using option s; see section 3.7.2 (Node position) of the Forest manual. In particular, s=0 will position it directly below (the anchor of) the parent.

However, for this option to take effect, it must be changed after Forest does its positioning magic (in pack stage) and before it computes the absolute coordinates of the nodes (in compute xy stage); see section 3.4.1 (Stages). This can be achieved using the temporal propagator before computing xy; see section 3.4.2 (Temporal propagators).

To achieve the OP's goal, we must therefore equip the offending node with before computing xy={s=0}, as shown below.

\documentclass{beamer}
\usepackage{forest}

\begin{document}
\begin{frame}
  \begin{forest}
    for tree = {
        draw, circle,
        s sep = 6mm
      }
      [
        [ [[,tier=base]] [] ]
        [ [] [,tier=base, before computing xy={s=0}] [] ]
      ]
  \end{forest}
\end{frame}
\end{document}
4
  • Why does it default to alignment beneath its sibling? Off-topic, if I may: how expensive is it to measure the height and depth of nodes? (prooftrees question)
    – cfr
    Apr 3, 2022 at 3:17
  • When forest packs the siblings, it first puts them it a "line" as close together as it can. But if two siblings do not overlap in the "y" dimension (assuming grow=south here), then their minimal distance is undefined. Aligning them vertically seemed the simplest solution. For example, centering the offending sibling between the two adjacent siblings cannot be done at the time yet, because the "s" of the following sibling is not known yet (the children are packed sequentially). Apr 3, 2022 at 6:43
  • @cfr One other option would be to figure out if the siblings form multiple horizontal "lines" and deal with those lines separately. But that complicates not just the packing of the siblings (the lines could "join" and "diverge") but also their alignment to the parent (each line would need to be caligned independently; effectively, we would end up with several primary children, and the UI would end up a nightmare I believe). Apr 3, 2022 at 6:43
  • @cfr On the off-topic question. The measurement itself is not expensive at all. But, as with everything else, retrieval is (pgfmath, argument processor). Apr 3, 2022 at 6:43

You must log in to answer this question.

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