5

I wanted to typeset

enter image description here

So, I wrote (derivation of https://tex.stackexchange.com/a/302713/82730)

\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}
      \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}

which was rendered as

enter image description here

The tree at the left is not vertically centered, but it's at the top. How can I vertically center the trees?

  • 1
    Try to put all of them in separate boxes. \begin{adjustbox}{valign=M} <content> \end{adjustbox}\qquad\begin{adjustbox}{valign=M} <content> \end{adjustbox}\qquad\begin{adjustbox}{valign=M} <content> \end{adjustbox}. – azetina May 13 '16 at 4:20
  • @azetina Your suggestion works, but I don't know why it works. Can you explain? – crocket May 13 '16 at 4:26
  • 1
    What happens here is that you are creating 3 boxes each of which can now have varied alignments. Unlike your first case, there is only one box but nothing to reference alignment with hence leaving it to top alignment. – azetina May 13 '16 at 4:28
  • @azetina Why don't you answer this question? I'll choose it. – crocket May 13 '16 at 4:29
5

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}
  • Do you mean putting each forest tree in a table cell? – crocket May 13 '16 at 4:43
  • Yes. That is a different approach. :) – azetina May 13 '16 at 4:43
5

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}

without boxes

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.

  • Totally forgot about TikZ internals. Great job. I only dealt with the issue in terms of the boxes. – azetina May 14 '16 at 15:44
  • @azetina Well, your solution works, which is always good. I just thought this was a bit easier/more straightforward. – cfr May 14 '16 at 20:44
  • It took a while to wrap my mind around /tikz/baseline=(current bounding box.center). What a monster TikZ is. – crocket May 16 '16 at 1:42

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.