如何绘制树的正式总和?

如何绘制树的正式总和?

我想画这样的东西:

在此处输入图片描述

如图所示,+号相对于树的高度居中。到目前为止,我使用了表格环境,但结果并不令人满意,我得到了如下结果:

在此处输入图片描述

有什么想法吗?提前谢谢您!


\documentclass{article}
\usepackage{forest}
\usetikzlibrary{decorations.pathreplacing,calc}
\forestset{solid nodes/.style={for tree={circle,draw,inner sep=1,fill=black}},
            dir/.style={for tree={grow=#1}},
            leaf/.style={label=$#1$},
            mytree/.style={solid nodes, for tree={grow=north,s sep=1cm}}}
\begin{document}
 \begin{tabular}{c c c } 
        \begin{forest} mytree
        [
        [,leaf=r_1] 
        [
        [,leaf=x^{(2)}] [,leaf=x]
        [
        [,leaf=x^{(1)}][,leaf=r_2]
        ]
        ]]]
        \end{forest} 
    &
    +
    &
        \begin{forest} mytree
        [
        [,leaf=r_1] 
        [
        [,leaf=x^{(2)}] [,leaf=x]
        [
        [,leaf=x^{(1)}][,leaf=r_2]
        ]
        ]]]
    \end{forest} 
    \\
    \end{tabular}
\end{document}

答案1

不涉及修改树本身的替代方法是使用包adjustbox

\documentclass{article}
\usepackage{forest}
\usepackage{adjustbox}
\usetikzlibrary{decorations.pathreplacing,calc}
\forestset{solid nodes/.style={for tree={circle,draw,inner sep=1,fill=black}},
            dir/.style={for tree={grow=#1}},
            leaf/.style={label=$#1$},
            mytree/.style={solid nodes, for tree={grow=north,s sep=1cm}}}
\begin{document}
\adjustbox{valign=c}{
        \begin{forest} mytree
        [
        [,leaf=r_1] 
        [
        [,leaf=x^{(2)}] [,leaf=x]
        [
        [,leaf=x^{(1)}][,leaf=r_2]
        ]
        ]]]
        \end{forest}
}
\adjustbox{valign=c}{
   $+$
 }
\adjustbox{valign=c}{
        \begin{forest} mytree
        [
        [,leaf=r_1] 
        [
        [,leaf=x^{(2)}] [,leaf=x]
        [
        [,leaf=x^{(1)}][,leaf=r_2]
        ]
        ]]]
    \end{forest} 
}
\end{document}

代码输出

答案2

您需要将选项写入baseline您希望与符号对齐的节点+

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{decorations.pathreplacing,calc}
\forestset{solid nodes/.style={for tree={circle,draw,inner sep=1,fill=black}},
            dir/.style={for tree={grow=#1}},
            leaf/.style={label=$#1$},
            mytree/.style={solid nodes, for tree={grow=north,s sep=1cm}}}
\usepackage{tabularray}

\begin{document}
 \[
        \begin{forest} mytree
        [
            [,leaf=r_1]
            [
                [,leaf=x^{(2)}, baseline]   % <---
                [,leaf=x]
                [
                    [,leaf=x^{(1)}]
                    [,leaf=r_2]
                ]
            ]
        ]
        \end{forest}
       +
        \begin{forest} mytree
        [
            [,leaf=r_1]
            [
                [,leaf=x^{(2)}, baseline]   % <---
                [,leaf=x]
                [
                    [,```leaf=x^{(1)}]
                    [,leaf=r_2]
                ]
            ]
        ]
        \end{forest}
 \]
\end{document}

在此处输入图片描述

相关内容