# Forest: Help with automatic anchoring of nodes relative to edges of other nodes

I have trees in forest, where each node in the tree is annotated with a superscript (index) and a subscript (outdex), which I instantiate as separate nodes and position in the left and right corner of each tree node. However, I have to issues I've been struggling with for a while.

1) Right now, those get anchored with respect to the center of the node, but I'd like to take the width of each node label into account. If you see my example, the output is ok with nodes with short (1 letter) labels, but gets uglier with longer labels. E.g. with forget, I'd like them to be places wrt the edges of the word, not the node center. I'd like to modify my definition of index and outdex, but I cannot figure out how to refer to the width of a node label.

Of course I could just change the placement manually for this single tree, but I need to generate these as the output of a Python script, so I am trying to come up with a fixed solution. One alternative I thought of would be to generate those annotations as ^\Index\Label_\Outdex instead of separate nodes, but then I'd have problems placing boxes around the outdex.

2) The other things should be super simple, but somehow I also can't figure this out. I'd like to change the boxes around some of those nodes, so that they are a bit smaller and don't overlap with the main node label. I tried changing width and inner separator (e.g. \tikzset{boxed/.style = {draw, minimum size=0em, inner sep=.05em}}), but nothing changes so I am guessing I am misunderstanding those commands. I tried rereading the manual but, alas, I am stuck.

I hope the question is clear! Any help would be greatly welcome!

\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}

• Can you tell your Python script to use \node[index] at (t1222121.north west) {18}; instead of \node[index] at (t1222121) {18};, say? (BTW, \tikzset{boxed/.style = {draw,inner sep=0.5pt}} does clearly make the boxes tighter.) – user194703 May 17 '20 at 14:04
• Oh <facepalm>. Yes! If I do that and get rid of the anchor option in the style definition, that's definitely better. Thank you! – Any May 17 '20 at 14:11
• Maybe \tikzset{annotation/.style = {font=\footnotesize,inner sep=0.5pt,outer sep=-2pt}} \tikzset{index/.style = {annotation, anchor= south}} \tikzset{outdex/.style = {annotation, anchor= north}} this looks even better? BTW, why don't you add the labels already in the tree? You can say something like label=above right:{...}. – user194703 May 17 '20 at 14:13
• Yes if I change the positioning as in your previous comment + this in the style it's definitely better. Thanks so much!! re:BTW Oh that's just because the "annotation" part and the "tree" part are used by two different routines in my python script. The latex output is just for presentation. I'll fix that in the future! – Any May 17 '20 at 14:24

If you change the anchors at which you position the nodes, play a bit with inner sep and outer sep, and increase the distance between the levels, the tree will be less crowded.

\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}