How can I vertically center forest trees?
As defined, there is only one box and no other reference for alignment hence all trees are top-aligned. Choosing to put each tree on separate boxes and choosing the middle alignment, M
the vertical center (equal height and depth), we obtain the desired result. Something like:
\begin{adjustbox}{valign=M} <content> \end{adjustbox}\qquad\begin{adjustbox}{valign=M} <content> \end{adjustbox}\qquad\begin{adjustbox}{valign=M} <content> \end{adjustbox}
Here is mwe:
\documentclass[a4size]{article}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\usepackage{adjustbox}
% forest
\usepackage{forest}
% parsing tree
\forestset{
declare toks={wff}{},
declare toks={connective}{},
declare dimen register={parsing tree sep},
parsing tree sep=7.5pt,
parsing tree/.style={
% append the current root to a new phantom root.
for root'={
replace by={[,phantom,append=!last dynamic node]}
},
for tree={
math content,
parent anchor=children,
child anchor=parent,
inner sep=0pt,
if n children=1{!first.before packing=calign with current edge}{},
delay={
content=\circ,
insert before/.wrap pgfmath arg={
[##1,no edge,math content,anchor=base east, before computing xy={
s/.pgfmath={s("!n")-parsing_tree_sep}}]
}{wff},
if connective={}{connective/.option=wff}{},
insert after/.wrap pgfmath arg={
[##1,no edge,math content,anchor=base west, before computing xy={
s/.pgfmath={s("!p")+parsing_tree_sep}}]
}{connective}
}
}
},
default preamble={parsing tree,baseline}
}
\begin{document}
\begin{center}
\fbox{
\begin{adjustbox}{valign=M}
\begin{forest}
[,wff=1,connective=\chi]
\end{forest}
\end{adjustbox}
\qquad
\begin{adjustbox}{valign=M}
\begin{forest}
[,wff=m+3,connective=\neg [,wff=m,connective=\chi]]
\end{forest}
\end{adjustbox}
\qquad
\begin{adjustbox}{valign=M}
\begin{forest}
[,wff=m+n+3,connective=\neg [,wff=m,connective=\chi]
[,wff=n,connective=\chi]]
\end{forest}
\end{adjustbox}
}
\end{center}
\end{document}
Other methods involve using a table format. Something like:
\begin{tabular}{m{0.25\textwidth}m{0.25\textwidth}m{0.25\textwidth}@{}m{0pt}@{}}
\begin{forest}
[,wff=1,connective=\chi]
\end{forest}
&
\begin{forest}
[,wff=m+3,connective=\neg [,wff=m,connective=\chi]]
\end{forest}
&
\begin{forest}
[,wff=m+n+3,connective=\neg [,wff=m,connective=\chi]
[,wff=n,connective=\chi]]
\end{forest}
&
\end{tabular}
You do not need different boxes etc. You just need to remove baseline
and use TikZ's baseline
option to centre the trees vertically. For example,
\documentclass[]{article}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\usepackage{adjustbox}
% forest
\usepackage{forest}
% parsing tree
\forestset{
declare toks={wff}{},
declare toks={connective}{},
declare dimen register={parsing tree sep},
parsing tree sep=7.5pt,
parsing tree/.style={
% append the current root to a new phantom root.
for root'={
replace by={[,phantom,append=!last dynamic node]}
},
for tree={
math content,
parent anchor=children,
child anchor=parent,
inner sep=0pt,
if n children=1{!first.before packing=calign with current edge}{},
delay={
content=\circ,
insert before/.wrap pgfmath arg={
[##1,no edge,math content,anchor=base east, before computing xy={
s/.pgfmath={s("!n")-parsing_tree_sep}}]
}{wff},
if connective={}{connective/.option=wff}{},
insert after/.wrap pgfmath arg={
[##1,no edge,math content,anchor=base west, before computing xy={
s/.pgfmath={s("!p")+parsing_tree_sep}}]
}{connective}
}
},
/tikz/baseline=(current bounding box.center)
},
default preamble={parsing tree}
}
\begin{document}
\begin{center}
\fbox{
\begin{adjustbox}{valign=M}
\begin{forest}
[,wff=1,connective=\chi]
\end{forest}
\qquad
\begin{forest}
[,wff=m+3,connective=\neg [,wff=m,connective=\chi]]
\end{forest}
\qquad
\begin{forest}
[,wff=m+n+3,connective=\neg [,wff=m,connective=\chi]
[,wff=n,connective=\chi]]
\end{forest}
\end{adjustbox}
}
\end{center}
\end{document}
Note that a4size
is not a valid option so I've removed it as it makes no difference. Perhaps you meant a4paper
? In that case, you should be sure to load something like geometry
with a standard class.