如何在树形图中的节点上添加标签?

如何在树形图中的节点上添加标签?

我需要在我的文档中创建一个特定的图表,但我无法做到。我已经有了大部分内容,但如果右侧已经有另一个子节点(蓝色省略号),我似乎无法在节点下方添加子节点。然后,如何让下面的这些子节点都处于同一级别?我还希望只有 1 个箭头,而其他子节点仅通过一条线连接。

在此处输入图片描述

这是我目前所拥有的:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{verbatim}

 \usetikzlibrary{shapes}
 \usepackage{amsmath}
\usepackage{xspace}
\newcommand{\A}{\ensuremath{\mathcal{A}}\xspace}
\newcommand{\B}{\ensuremath{\mathcal{B}}\xspace}
\newcommand\pa[1]{\ensuremath{\left(#1\right)}}
\begin{document}
\begin{tikzpicture}[grow=right,
level 1/.style={sibling distance=3.5cm,level distance=4.5cm},
level 2/.style={sibling distance=2cm, level distance=4cm},
level 3/.style={sibling distance=2cm, level distance=3.5cm},
level 4/.style={sibling distance=2cm, level distance=2.5cm},
level 5/.style={sibling distance=2cm, level distance=2.5cm},
edge from parent/.style={very thick,draw=blue!40!black!60,
    shorten >=5pt, shorten <=5pt},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
kant/.style={text width=2cm, text centered, sloped},
every node/.style={text ragged, inner sep=2mm},
punkt/.style={rectangle, rounded corners, shade, top color=white,
bottom color=blue!50!black!20, draw=blue!40!black!60, very
thick }
]

\node[punkt, text width=5.5em] {*/aper-i-/}
%Lower part lv1
child {
    node[punkt, text width=6em] {\hspace{10pt}*/a'pert-/}
     %child 2
    child {
        node [punkt]{
            \textbf{*/apert-/}
        }
        child {
        node [punkt]{
            \textbf{*/-ur-/}
        }
        child {
        node [punkt]{
            \textbf{*/-a/}
        }}}}
        edge from parent{
            node[kant, below] {thème 1}}
}
child {
    node[punkt] [text ragged] {
        \textbf{*/'aperu-/}
    }
    edge from parent
        node[kant, below, pos=.6] {thème 2}
}
%Upper part, lv1
child {
    node[punkt, text width=6em] {\hspace{10pt}*/aper-/}
        edge from parent{
            node[kant, below] {thème 1}}
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

编辑:

  • 你的“树”更容易画matrix
  • 多行节点必须明确写为带有名称的节点
  • 填充颜​​色可以局部确定

第一次尝试:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix, quotes, shapes}

\begin{document}
\begin{tikzpicture}[
every node/.style = {font=\ttfamily, align=left, inner sep=2mm},
     punkt/.style = {rectangle, rounded corners,
            draw=blue!40!black!60, very thick,
            top color=white, bottom color=blue!50!black!20,
            %                shade,
                    },
every edge quotes/.append style = {auto=left, font=\footnotesize\sffamily,
                                   anchor=south, sloped},
]
\matrix (m) [matrix of nodes,
%             nodes in empty cells,
             nodes={punkt},
             column sep=12mm, row sep=6mm,
             column 1/.style = {column sep=33mm},
             row 4/.style = {nodes={ellipse, fill=blue!30,
                             anchor=north, inner xsep=0mm}}]
{
*/a'pert-/  & */apert-/     &               &           &               \\
            & */'aperu-/    &               &           &               \\
            & */'aper-/     & */'aper-i-/   & */-ur-/   & */-a/         \\
base        & themes        & \node{radical selectionne\\
                              parmi les different\\
                              themes};
                                            & norphene suffixal
                                                        & \node{morphene flexionnel\\
                                                          marquant la categorie\\
                                                          gramaticale};   \\
};
\draw   (m-1-1)  to ["thème 1"]     (m-1-2)
        (m-1-1)  to ["thème 2"]     (m-2-2)
        (m-1-1)  to ["thème 1"]     (m-3-2);
\draw   (m-3-2) -- (m-3-3)
        (m-3-3) -- (m-3-4)
        (m-3-4) -- (m-3-5);
\draw   (m-1-1) -- (m-4-1)
        (m-3-2) -- (m-4-2);
\end{tikzpicture}
\end{document}

第二次尝试:

在此处输入图片描述

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix, quotes, shapes}

\usepackage[utf8]{inputenc}
\begin{document}


\begin{tikzpicture}[
punkt/.style = {top color=white, bottom color=blue!50!black!20},
pnktr/.style = {fill=red!20},
every edge quotes/.style = {auto=left, font=\footnotesize\sffamily,
                            anchor=south, sloped},
                    ]
\matrix (m) [matrix of nodes,
             nodes={rectangle, rounded corners,
                    draw=blue!40!black!60, very thick,
                    font=\ttfamily, align=left, inner sep=2mm,
                    },
             column sep=6mm, row sep=8mm,
             column 1/.style = {column sep=33mm},
             row 4/.style = {nodes={ellipse, draw=blue!40!black!60, very thick,
                                    fill=cyan!20, anchor=north, inner xsep=0mm}}
             ]
{
|[punkt]| */a'pert-/  
    & |[punkt]| */apert-/   &   &   &               \\
    & |[punkt]| */'aperu-/  &   &   &               \\
    & |[punkt]| */'aper-/   
        & |[pnktr]| */'aper-i-/ 
                            & |[pnktr]| */-ur-/ 
                                & |[pnktr]| */-a/   \\
base        
    & themes                
        & \node (m-4-3)
          {radical selectionne\\
          parmi les different\\
          themes};          & morphene suffixal
                                & \node (m-4-5)
                                  {morphene flexionnel\\
                                  marquant la categorie\\
                                  gramaticale};   \\
};
\draw   (m-1-1)  edge ["thème 1"]     (m-1-2)
        (m-1-1)  edge ["thème 2"]     (m-2-2)
        (m-1-1)  edge ["thème 1"]     (m-3-2);
\draw   (m-3-2) -- (m-3-3)
        (m-3-3) -- (m-3-4)
        (m-3-4) -- (m-3-5);
\draw   (m-1-1) -- (m-4-1)
        (m-3-2) -- (m-4-2)
        (m-3-3) -- (m-4-3)
        (m-3-4) -- (m-4-4)
        (m-3-5) -- (m-4-5);
\end{tikzpicture}
\end{document}

答案2

你可以用这个positioning库来做这件事。我不是 Tikz 专家,但这可能会给你一个起点。如果你想要装饰标签,那么你可以像任何其他节点一样添加样式。而且你需要修复节点的水平间距,为标签腾出空间。

在此处输入图片描述

注意:我使用xelatexunicode重音字符来编译它。

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{verbatim}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\usepackage{amsmath}
\usepackage{xspace}
\newcommand{\A}{\ensuremath{\mathcal{A}}\xspace}
\newcommand{\B}{\ensuremath{\mathcal{B}}\xspace}
\newcommand\pa[1]{\ensuremath{\left(#1\right)}}
\begin{document}
\begin{tikzpicture}[grow=right,
level 1/.style={sibling distance=3.5cm,level distance=4.5cm},
level 2/.style={sibling distance=2cm, level distance=4cm},
level 3/.style={sibling distance=2cm, level distance=3.5cm},
level 4/.style={sibling distance=2cm, level distance=2.5cm},
level 5/.style={sibling distance=2cm, level distance=2.5cm},
edge from parent/.style={very thick,draw=blue!40!black!60,
    shorten >=5pt, shorten <=5pt},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
kant/.style={text width=2cm, text centered, sloped},
every node/.style={text ragged, inner sep=2mm},
punkt/.style={rectangle, rounded corners, shade, top color=white,
bottom color=blue!50!black!20, draw=blue!40!black!60, very
thick }
]

    \node[punkt, text width=5.5em] (T1) {*/aper-i-/}
%Lower part lv1
child {
    node[punkt, text width=6em] (T2) {\hspace{10pt}*/a'pert-/}
     %child 2
    child {
        node [punkt] (T3) {
            \textbf{*/apert-/}
        }
        child {
            node [punkt] (T4) {
            \textbf{*/-ur-/}
        }
        child {
            node [punkt] (T5) {
            \textbf{*/-a/}
        }}}}
        edge from parent{
            node[kant, below] {thème 1}}
}
child {
    node[punkt] [text ragged] {
        \textbf{*/'aperu-/}
    }
    edge from parent
        node[kant, below, pos=.6] {thème 2}
}
%Upper part, lv1
child {
    node[punkt, text width=6em] {\hspace{10pt}*/aper-/}
        edge from parent{
            node[kant, below] {thème 1}}
};

    \node[below = 4.5cm of T1] {Base};
    \node[below =of T2] {Thèmes};
    \node[below =of T3, text width=3cm] {Radical selectionné parmi les autres thèmes};
    \node[below =of T4, text width=3cm] {Morphème suffixal};
    \node[below =of T5, text width=3cm] {Morphème flexionnel marquant la whatever};

\end{tikzpicture}
\end{document}

我补充道:

\usetikzlibrary{positioning}

然后像这样命名相关节点:

\node[punkt, text width=5.5em] (T1) {*/aper-i-/}

名称是(括号)中的位。

然后我可以像这样定义另一个节点:

\node[below = 4.5cm of T1] {Base};

相关内容