森林:帮助相对于其他节点的边缘自动锚定节点

森林:帮助相对于其他节点的边缘自动锚定节点

我在森林中种植了树木,树中的每个节点都用上标(索引)和下标(外标)标注,我将其实例化为单独的节点,并放置在每个树节点的左角和右角。然而,我遇到了一些困扰我一段时间的问题。

1) 目前,这些标签相对于节点中心固定,但我想考虑每个节点标签的宽度。如果您看到我的示例,则对于带有较短(1 个字母)标签的节点,输出是可以的,但对于较长的标签,输出会变得更丑。例如忘记,我希望它们位于单词的边缘,而不是节点中心。我想修改 index 和 outdex 的定义,但我不知道如何引用节点标签的宽度。

当然,我可以手动更改这棵树的放置位置,但我需要将它们作为 Python 脚本的输出生成,所以我试图想出一个固定的解决方案。我想到的一个替代方案是生成这些注释而不是^\Index\Label_\Outdex单独的节点,但这样我就无法在 outindex 周围放置框。

2) 其他事情应该非常简单,但不知何故我还是搞不懂。我想更改一些节点周围的框,使它们小一点,不与主节点标签重叠。我尝试更改宽度和内部分隔符(例如\tikzset{boxed/.style = {draw, minimum size=0em, inner sep=.05em}}),但没有任何变化,所以我猜我误解了这些命令。我尝试重新阅读手册,但唉,我被卡住了。

希望问题已经清楚!任何帮助都欢迎!

\documentclass{article}

% load tikz and forest with more visible arrows
\usepackage{tikz}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

% define annotations <- THESE ARE THE THINGS I AM TRYING TO FIX
\tikzset{annotation/.style = {font=\footnotesize}}
\tikzset{index/.style = {annotation, anchor= south east}}
\tikzset{outdex/.style = {annotation, anchor= north west}}
\tikzset{boxed/.style = {draw}}
\tikzset{empty/.style = {}}
\tikzset{non-final/.style = {opacity=70}}


\begin{document}

\begin{forest}
%
%%%%%%%%
% Tree %
%%%%%%%%
%
                            [$v$P, name=t1222121
                                [DP, name=subj
                                    [the, name=t122212111]
                                    [lawyer, name=t122212112]
                                ]
                                [$v$', name=t12221212
                                    [$v2$, name=t122212121]
                                    [VP, name=t122212122
                                        [forgets, name=t1222121221]
                                        [DP, name=t1222121222
                                            [his, name=t12221212221]
                                            [briefcase, name=t12221212222]
                                        ]
                                    ]
                                ]
                            ]
%%%%%%%%%%%%%%%
% Annotations %
%%%%%%%%%%%%%%%
%
\node[index]            at (t1222121)                {18};
\node[outdex]           at (t1222121)                {19};
%
\node[index]            at (subj)                    {19};
\node[outdex]           at (subj)                    {20};
%
\node[index]            at (t122212111)              {20};
\node[outdex]           at (t122212111)              {21};
%
\node[index]            at (t122212112)              {20};
\node[outdex]           at (t122212112)              {22};
%
\node[index]            at (t12221212)               {19};
\node[outdex, boxed]    at (t12221212)               {24};
%
\node[index]            at (t122212121)              {24};
\node[outdex]           at (t122212121)              {25};
%
\node[index]            at (t122212122)              {24};
\node[outdex]           at (t122212122)              {26};
%
\node[index]            at (t1222121221)             {26};
\node[outdex]           at (t1222121221)             {27};
%
\node[index]            at (t1222121222)             {26};
\node[outdex]           at (t1222121222)             {28};
%
\node[index]            at (t12221212221)            {28};
\node[outdex]           at (t12221212221)            {29};
%
\node[index]            at (t12221212222)            {28};
\node[outdex]           at (t12221212222)            {30};
\end{forest}

\end{document}

答案1

如果你改变节点的定位锚点,稍微调整一下inner sepouter sep,并增加级别之间的距离,那么树就不会那么拥挤。

\documentclass{article}

% load tikz and forest with more visible arrows
\usepackage{forest}
\usetikzlibrary{arrows.meta}

% define annotations <- THESE ARE THE THINGS I AM TRYING TO FIX
\tikzset{annotation/.style = {font=\footnotesize,inner sep=0.5pt,outer sep=-2pt}}
\tikzset{index/.style = {annotation, anchor= south}}
\tikzset{outdex/.style = {annotation, anchor= north}}
\tikzset{boxed/.style = {draw}}
\tikzset{empty/.style = {}}
\tikzset{non-final/.style = {opacity=70}}


\begin{document}

\begin{forest}
%
%%%%%%%%
% Tree %
%%%%%%%%
%
for tree={where level=0{l sep+=2.5em}{l sep+=0.5em}},
                            [$v$P, name=t1222121
                                [DP, name=subj
                                    [the, name=t122212111]
                                    [lawyer, name=t122212112]
                                ]
                                [$v$', name=t12221212
                                    [$v2$, name=t122212121]
                                    [VP, name=t122212122
                                        [forgets, name=t1222121221]
                                        [DP, name=t1222121222
                                            [his, name=t12221212221]
                                            [briefcase, name=t12221212222]
                                        ]
                                    ]
                                ]
                            ]
%%%%%%%%%%%%%%%
% Annotations %
%%%%%%%%%%%%%%%
%
\node[index]            at (t1222121.north west)                {18};
\node[outdex]           at (t1222121.south east)                {19};
%
\node[index]            at (subj.north west)                    {19};
\node[outdex]           at (subj.south east)                    {20};
%
\node[index]            at (t122212111.north west)              {20};
\node[outdex]           at (t122212111.south east)              {21};
%
\node[index]            at (t122212112.north west)              {20};
\node[outdex]           at (t122212112.south east)              {22};
%
\node[index]            at (t12221212.north west)               {19};
\node[outdex, boxed]    at (t12221212.south east)               {24};
%
\node[index]            at (t122212121.north west)              {24};
\node[outdex]           at (t122212121.south east)              {25};
%
\node[index]            at (t122212122.north west)              {24};
\node[outdex]           at (t122212122.south east)              {26};
%
\node[index]            at (t1222121221.north west)             {26};
\node[outdex]           at (t1222121221.south east)             {27};
%
\node[index]            at (t1222121222.north west)             {26};
\node[outdex]           at (t1222121222.south east)             {28};
%
\node[index]            at (t12221212221.north west)            {28};
\node[outdex]           at (t12221212221.south east)            {29};
%
\node[index]            at (t12221212222.north west)            {28};
\node[outdex]           at (t12221212222.south east)            {30};
\end{forest}

\end{document}

在此处输入图片描述

相关内容