4

I'm new to the forest package and created a tree with a node between two nodes, that is not centered. In the picture, it's the ZS(A; C) below root. This is the code:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{forest}

\begin{document}
\tikzset{>=latex}

\begin{forest} for tree={align=center}
[TH(3; A; B; C), for children={l+=2ex}
    [TH(2; A; C; B) 
        [TH(1; A; B; C)
            [ZS(A; C)]
        ]
        [ZS(A; B)]
        [TH(1; C; A; B)
            [ZS(C; B)]
        ]
    ] 
    [ZS(A; C)]
    [TH(2; B; A; C)
        [TH(1; B; C; A)
            [ZS(B; A)]
        ]
        [ZS(B; C)]
        [TH(1; A; B; C
            [ZS(A; C)]
        ]
    ]
]
\end{forest}

\end{document}

What am I doing wrong?

enter image description here

0

1 Answer 1

4

This is due to the way forest calculates the node positions, as described on p. 43f of the manual:

For example, consider the manual correction below. By default, B is closer to A than C because packing proceeds from the first to the last child — the position of B would be the same if there was no C. Adjusting s at the right moment, it is easy to center B between A and C.

It goes on to suggest adding before computing xy={s/.average={s}{siblings}} to the non-centred node, like so:

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{forest}

\begin{document}
\tikzset{>=latex}

\begin{forest} for tree={align=center}
[TH(3; A; B; C), for children={l+=2ex}
    [TH(2; A; C; B) 
        [TH(1; A; B; C)
            [ZS(A; C)]
        ]
        [ZS(A; B)]
        [TH(1; C; A; B)
            [ZS(C; B)]
        ]
    ] 
    [ZS(A; C), before computing xy={s/.average={s}{siblings}}]
    [TH(2; B; A; C)
        [TH(1; B; C; A)
            [ZS(B; A)]
        ]
        [ZS(B; C)]
        [TH(1; A; B; C
            [ZS(A; C)]
        ]
    ]
]
\end{forest}

\end{document}

compiled output with centred leaf

2
  • @cfr This seems to align the root node and the middle node, but it does so by shifting the root node to the current position of the middle node, so that both are not centred. Or am I missing something here?
    – diabonas
    Commented Oct 10, 2017 at 11:15
  • I was probably just too sleepy - sorry.
    – cfr
    Commented Oct 10, 2017 at 13:11

You must log in to answer this question.

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