如何在森林中的树中的节点下方添加多行文本

如何在森林中的树中的节点下方添加多行文本

我想制作一棵树,其子节点下方居中显示项目符号列表,如下所示:

我想要的树形图像

我在 TeX SE 上搜索过,找到了一些问题,这些问题描述了如何在节点下方放置标签,但对于多行文本或列表,这种方法不太适用。这是我的 MWE(它在 TexStudio 中生成 pdf,尽管我得到了一些残留错误):

\documentclass[11pt, letterpaper,twoside, fleqn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{forest}

\begin{document}
    \begin{figure}
        \begin{center}
            \begin{forest}
                for tree={
                    myleaf/.style={label=below:{\strut#1}},
                    s sep=5cm
                }
                [Root Question,rectangle,rounded corners,draw
                    [Yes,rectangle,rounded corners,draw,align=center,
                        myleaf={$\bullet$ Subsequent question?\\
                                $\bullet$ Subsequent longer\\
                                    question?}
                    ]
                    [No,rectangle,rounded corners,draw]
                ]
                \node[above=30pt,align=center,anchor=center] {Tree Title};
            \end{forest}
        \end{center}
        \label{fig_tree1}
    \end{figure}    
\end{document}

当前坏 mwe 树

我对 Latex 树还很陌生,到目前为止我一直在使用森林,但我并不反对 TikZ 解决方案。


顺便提一下,我最终还想将两棵树并排放在一个图中(如下所示,但“后续问题”文本稍大一些)。我也需要一些指导。

两棵树并排

答案1

您需要这样说align=left或做类似的事情,以便允许节点中有多行文本。(标签也是一个节点。)

\documentclass[11pt, letterpaper,twoside, fleqn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{forest}

\begin{document}
    \begin{figure}
        \begin{center}
            \begin{forest}
                for tree={
                    myleaf/.style={label={[align=left]below:{\strut#1}}},
                    s sep=5cm
                }
                [Root Question,rectangle,rounded corners,draw
                    [Yes,rectangle,rounded corners,draw,align=center,
                        myleaf={$\bullet$ Subsequent question?\\
                                $\bullet$ Subsequent longer\\
                                    question?}
                    ]
                    [No,rectangle,rounded corners,draw]
                ]
                \node[above=30pt,align=center,anchor=center] {Tree Title};
            \end{forest}
        \end{center}
        \label{fig_tree1}
    \end{figure}    
\end{document}

在此处输入图片描述

itemize如果您指定,也可以使用text width。这也解决了您的第二个问题。

\documentclass[11pt, letterpaper,twoside, fleqn]{article}
\usepackage[margin=1in]{geometry}
\usepackage{subcaption}
\usepackage{floatrow}
\usepackage{forest}
\usetikzlibrary{positioning}
\tikzset{leaflet/.style={align=left,text width=3.14cm}}
\begin{document}
\begin{figure}[htb]
\floatsetup{valign=t, heightadjust=all}
\ffigbox{%
\begin{subfloatrow}
\ffigbox{\begin{forest}
                for tree={
                    s sep=3.14cm
                }
                [Root Question,rectangle,rounded corners,draw,alias=root
                    [Yes,rectangle,rounded corners,draw,alias=LL]
                    [No,rectangle,rounded corners,draw,alias=LR]
                ]
                \node[above=12pt of root,align=center,anchor=center] {LeftTree Title};
                \node[below=1pt of LL,leaflet]
                {\begin{itemize}\setlength\itemsep{0pt}
                                \item Subsequent question?
                                \item Subsequent longer
                                    question?
                                \end{itemize}};
                \node[below=1pt of LR,leaflet]
                {\begin{itemize}\setlength\itemsep{0pt}
                                \item Subsequent question?
                                \item Subsequent longer
                                    question?
                                \end{itemize}};                             
            \end{forest}}{\caption{Left.\label{fig:left}}}
\ffigbox{\begin{forest}
                for tree={
                    s sep=3.14cm
                }
                [Root Question,rectangle,rounded corners,draw,alias=root
                    [Yes,rectangle,rounded corners,draw,alias=RL]
                    [No,rectangle,rounded corners,draw,alias=RR]
                ]
                \node[above=12pt of root,align=center,anchor=center] {Right Tree Title};
                \node[below=1pt of RL,leaflet]
                {\begin{itemize}\setlength\itemsep{0pt}
                                \item Subsequent question?
                                \item Subsequent longer
                                    question?
                                \end{itemize}};
                \node[below=1pt of RR,leaflet]
                {\begin{itemize}\setlength\itemsep{0pt}
                                \item[{}] Hibernate?
                                \end{itemize}};                             
            \end{forest}}{\caption{Right.\label{fig:right}}}
\end{subfloatrow}}
{\caption{Trees.}\label{fig:Trees}}
\end{figure}
\end{document}

在此处输入图片描述

如您所见,现在内容增加了一倍:有标题和说明文字。我保留了两者,因为删除其中一个可能比添加一个更容易。

相关内容