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

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

  • @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 Oct 10 '17 at 11:15
  • I was probably just too sleepy - sorry. – cfr Oct 10 '17 at 13:11

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.