[Tex/LaTex] Aligning several forest trees in centered way

forestlinguisticsvertical alignment

I want to align three trees in the way shown in the following figure:

enter image description here

What I have now is the following code:

\documentclass{minimal}   

\usepackage{forest}

\forestset{
tag/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top},where
  n children=0{}{}}
}


\begin{document}


\hfill
\begin{forest}
tag
[NP
        [John]]
\end{forest}
\hfill
\begin{forest}
tag
[S
        [NP$\downarrow$]
        [VP
                [V
                        [laughs]]]]
\end{forest}
\hfill
\begin{forest}
tag
[VP
        [ADV
                [always]]
        [VP*]]
\end{forest}
\hfill\mbox{}

\end{document}

I know of the baseline attribute, but this would not do real centering. Before using forest I just used tables and this got the alignment right automatically.

Best Answer

I can think of two ways of accompishing this without external help. Both use the fact that tikz/pgf keeps track of the picture's bounds in a special node called current bounding box.

  1. Use tikz's baseline option, which must be given as an argument to tikzpicture environment. This can be achieved by changing forest's option begin draw (at any node, but probably in the preamble)

  2. Use pgf's \pgfsetbaselinepointlater, which can be injected from forest' by using forest's tikz option (again, at any node, but probably in the preamble).

In your example, use this definition of tag style (the first way is active, the second way is commented out):

\forestset{tag/.style={
    for tree={parent anchor=south, child anchor=north,align=center,base=top},where n children=0{}{},
    begin draw/.code={\begin{tikzpicture}[baseline=(current bounding box.center)]},
    %tikz+={\pgfsetbaselinepointlater{\pgfpointanchor{current bounding box}{center}}},
  }}
Related Question