如何绘制节点间具有指定宽度的树

如何绘制节点间具有指定宽度的树

下面是我想要的图片 在此处输入图片描述

第一级单词:规则

第 2 级单词:长度、图案、弧线

三级单词:T1 T2 A1

4级单词:term_index term pos_tag ne_tag term_index term pos_tag ne_tag start_index end_index semantic_type

底线字符串:2 [1][][][LOC] [2][][][LOC] -> 1-2:loc-cons

下面是我当前的代码,以及它生成的图片。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}

\pagestyle{empty}

\begin{tikzpicture}[edge from parent fork down]

\tikzstyle{level 2}=[level distance=1.3cm, sibling distance=10cm]
\tikzstyle{level 3}=[level distance=2.0cm, sibling distance=5cm]
\tikzstyle{level 3}=[level distance=2.0cm, sibling distance=3cm]
\tikzstyle{r}=[rotate=45]

\node {Rule}
    child {node {Length}
        child {node {2}}
    }
    child {node {Pattern}
        child {node {$T_1$}
            child {node[r] {term\_index}}
            child {node[r] {term}}
            child {node[r] {pos\_tag}}
            child {node[r] {ne\_tag}}
        }
        child {node {$T_2$}
            child {node[r] {term\_index}}
            child {node[r] {term}}
            child {node[r] {pos\_tag}}
            child {node[r] {ne\_tag}}
        }
    }
    child {node {Arcs}
        child {node {$A_1$}
            child {node[r] {start\_index}}
            child {node[r] {end\_index}}
            child {node[r] {semantic\_type}}
        }
    };


%    \node{2[][][][LOC] [2][][][LOC] \rightarrow 1-2:loc-cons};

\end{tikzpicture}

\end{document}

在此处输入图片描述

谁能帮助我,非常感谢!

答案1

由于您被困在底线的设计中,我认为从这个底线构建树更容易,而不使用树包,而是使用级别和重心坐标系之间的预定义距离。

我将底线的标签放在下面而不是上面,因为这样对我来说看起来更清晰。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning} 

\begin{document}
\begin{tikzpicture}

% place bottom line
\node (n00) {2};
\foreach \str/\l/\comment [count=\num]in {%
[1]/1cm/term\_index,
[\,]/1mm/term,
[\,]/1mm/pos\_tag,
[LOC]/1mm/ne\_tag,
[2]/1cm/term\_index,
[\,]/1mm/term,
[\,]/1mm/pos\_tag,
[LOC]/1mm/ne\_tag,
1-/2cm/start\_index,
2:/1mm/end\_index,
loc-cons/1mm/semantic\_type}
{
    \pgfmathtruncatemacro{\leftnum}{\num-1}
    \node[right=\l of n0\leftnum] (n0\num) {\str};
    \node[rotate=45,below left=0mm of n0\num.center] (c\num) {\textcolor{gray}{\sffamily\comment}};
}

% distance between two levels
\def\distance{3cm}

% level 1
\node (n10) at ($(n00)+(0,\distance)$) {};
\node (n11) at ($(barycentric cs:n01=1,n02=1,n03=1,n04=1)+(0,\distance)$) {$T_1$};
\node (n12) at ($(barycentric cs:n05=1,n06=1,n07=1,n08=1)+(0,\distance)$) {$T_2$};
\node (n13) at ($(barycentric cs:n09=1,n010=1,n011=1)+(0,\distance)$) {$A_1$};

% level 2
\node (n20) at ($(n10)+(0,\distance)$) {Length};
\node (n21) at ($(barycentric cs:n11=1,n12=1)+(0,\distance)$) {Pattern};
\node (n22) at ($(n13)+(0,\distance)$) {Arcs};

% level 3
\node (n30) at ($(barycentric cs:n20=1,n21=1,n22=1)+(0,\distance)$) {Rule};

% links
\draw (n00) -- (n20);
\foreach \num in {1,...,4}
    \draw ($(n0\num.north)+(0,1mm)$) |- ($(n11)+(0,-1)$);
\draw ($(n11)+(0,-1)$) -- (n11);
\foreach \num in {5,...,8}
    \draw ($(n0\num.north)+(0,1mm)$) |- ($(n12)+(0,-1)$);
\draw ($(n12)+(0,-1)$) -- (n12);
\foreach \num in {9,...,11}
    \draw ($(n0\num.north)+(0,1mm)$) |- ($(n13)+(0,-1)$);
\draw ($(n13)+(0,-1)$) -- (n13);
\draw (n11) |- ($(n21)+(0,-1)$) -- (n21);
\draw (n12) |- ($(n21)+(0,-1)$);
\draw (n13) -- (n22);
\foreach \num in {0,...,2}
    \draw ($(n2\num.north)+(0,1mm)$) |- ($(n30)+(0,-1)$);
\draw ($(n30)+(0,-1)$) -- (n30);
\end{tikzpicture}

\end{document}

上面的代码生成的结果如下:

那个树

相关内容