Latex tikzpicture 将标签添加到层次结构分支

Latex tikzpicture 将标签添加到层次结构分支

我有一个层次结构:

\begin{figure}[!h]
\begin{center}
\resizebox{\textwidth}{!}{%
\begin{tikzpicture}[level 1/.style={sibling distance=7cm},
level 2/.style={sibling distance=2cm}, 
level 3/.style={sibling distance=2.5cm}, align = flush center]
    \tikzstyle{every node}=[draw,rectangle]

    \node {Root}
        [style=edge from parent fork down]
        child { 
            node {1} 
            child {node {1.1} 
                child {node {1.1.1} }
                child {node {1.1.2} }
                child {node {1.1.3} }
                }
            child {node {1.2}}
            child {node {1.3}}
        }
        child { 
            node {2} 
            child {node {2.1}}
            child {node {2.2} 
                child {node {2.2.1} }
                child {node {2.2.2} }
                child {node {2.2.3} }
                }
            child {node {2.3}}
        }
    ;
\end{tikzpicture} 
}
\end{center}
\end{figure}

我想知道,我该如何更改我的乳胶,以便在图形的某些分支上贴上标签?例如,单词“left”将位于“Root”和“1”之间的分支上,单词“right”将位于“Root”和“2”之间的分支上。谢谢。

答案1

这可以通过 来完成edge from parent node。请注意 已\tikzstyle被弃用。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level 1/.style={sibling distance=7cm},
level 2/.style={sibling distance=2cm}, 
level 3/.style={sibling distance=2.5cm}, align = flush center]
\tikzset{every node/.append style={draw,rectangle}}

    \node {Root}
        [style=edge from parent fork down]
        child { 
            node {1}    
            child {node {1.1} 
                child {node {1.1.1} }
                child {node {1.1.2} }
                child {node {1.1.3} }
                }
            child {node {1.2}}
            child {node {1.3}}
        edge from parent node[pos=0.25,above,draw=none]{left}%<-added
        } 
        child { 
            node {2} 
            child {node {2.1}}
            child {node {2.2} 
                child {node {2.2.1} }
                child {node {2.2.2} }
                child {node {2.2.3} }
                }
            child {node {2.3}}
        edge from parent node[pos=0.25,above,draw=none]{right}%<-added
        }
    ;
\end{tikzpicture} 
\end{document}

在此处输入图片描述

有了森林,这项事业可以说变得更加简单。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={grow=south,draw,s sep=2em},
forked edges
[Root
 [1,edge label={node[pos=0.35,above]{left}}
  [1.1
   [1.1.1]
   [1.1.2]
   [1.1.3]
  ]
  [1.2] 
  [1.3] 
 ]
 [2,edge label={node[pos=0.35,above]{right}}
  [2.1]
  [2.2
   [2.2.1]
   [2.2.2]
   [2.2.3]
  ] 
  [2.3] 
 ]
] 
\end{forest}
\end{document}

在此处输入图片描述

相关内容