# Relating tree nodes in forest to content in a table

I want to draw the following figure:

I now converted my trees to forest and the following code is what I have:

\documentclass{article}

\usepackage{forest}

\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top},
where n children=0{tier=word}{}
},
% auxiliary nodes without node label
empty nodes/.style={
delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}}
}

\begin{document}

\begin{figure}[htbp]
\centerline{%
\begin{tabular}{@{}l@{}}
\begin{forest}
sn edges,empty nodes
[CP
[{} [XP]]
[C$'$ [{} [C$^0$]]
[IP [{} [XP]]
[I$'$
[VP
[V$^0$]]
[{} [I$^0$]]]]]]
\end{forest}\\
\begin{tabular}{|l|l|l|l|l|}
\hline
%
SpecCP   & C0      & \multicolumn{2}{l|}{IP (without I0, V0)} & V0, I0\\
prefield & left SB     & \multicolumn{2}{l|}{middlefield}                 & right SB\\\cline{3-4}
&             & SpecIP           & phrases inside       &          \\
&             & subject position & the VP               &\\\hline
\end{tabular}
\end{tabular}}
\caption{\label{Abb-GB-Topo}CP, IP and VP and the topological model of German}
\end{figure}

\end{document}


This code generates:

The first figure was done with pstricks more or less by hand and I wonder whether there are better methods to ensure that XP is in the middle above the cells SpecCP and SpecIP and that the triangle is about to end at the cell saying Phrasen innerhalb der VP.

Edit:

Following cfr's suggestion, I started to work with the following code:

\documentclass{article}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top}},
terminus/.style={tier=word, for tree={fit=band}, for descendants={no path, align=left, l sep=0pt}},
empty nodes/.style={
delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}},
no path/.style={edge path={}},
set me left/.style={calign with current edge, child anchor=north west, for parent={parent anchor=south west}},
}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{forest}
sn edges,empty nodes
[CP
[{}
[XP,terminus
[SpecCP\\prefield, name=prefield
]
]
]
[C$'$
[{}
[C$^0$, terminus
[C0\\left SB
]
]
]
[IP
[{}
[XP, terminus
[{IP (without I0, V0)\\middlefield}, name=middlefield
[SpecIP\\subject position, set me left, name=subject
]
[phrases inside\\the VP, name=insidevp
]
]
]
]
[I$'$
[VP, name=vp
[V$^0$, name=v0, terminus, no path
[{V0, I0\\right SB}, name=p2
]
]
]
[{}
[I$^0$, terminus
]
]
]
]
]
]
% these are the table borders at the top and at the bottom
\draw [thick]
(prefield.north west) -- (p2.north east)
(prefield.west |- insidevp.south) -- (p2.east |- insidevp.south);
\draw
% the horizontal line in the middle
% for reasons I do not understand, the middlefield node does not extend over the phrases
% inside VP' node.
%(middlefield.south west) -- (middlefield.south east)
(subject.north west) -- (insidevp.north east)
(vp.south) -- (v0.center -| insidevp.west) -- (v0.west)
(v0.east) -- +(2.5pt,0) -- (vp.south)
;
\end{forest}
\caption{\label{Abb-GB-Topo}CP, IP and VP and the topological model of German}
\end{figure}

\end{document}


I replaced p1-p3 by more telling nodenames like middlefield and drew a shorter line that markes the middlefield area. I tried to do this with (middlefield.south west) -- (middlefield.south east)but somehow this does not work. The line is too short although the middle field node dominates both the nodes for SpecIP and phrases inside the VP.

There is a further question: V0 and I0 are part of the right sentence bracket, that is, these two nodes should dominate the explanation node for V0 and I0. I think the simplest way to achieve this may be to shift the node for right SB to the right, so that it lands below I0. But maybe there is a better way.

As for the vertical lines, I would have to introduce fake nodes below prefield, left SB and right SB and connect their right south corner to the right north corner of the node above, wouldn't I?

The following is what I tried, but the middlefield node is too short and the second vertical line is not in the middle between the left SB and middlefield node but nearer to the middlefiled node since I refer to the coordinates of the latter node.

\documentclass{article}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top}},
terminus/.style={tier=word, for tree={fit=band}, for descendants={no path, align=left, l sep=0pt}},
empty nodes/.style={
delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}},
no path/.style={edge path={}},
set me left/.style={calign with current edge, child anchor=north west, for parent={parent anchor=south west}},
}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{forest}
sn edges,empty nodes
[CP
[{}
[XP,terminus
[SpecCP\\prefield, name=prefield
]
]
]
[C$'$
[{}
[C$^0$, terminus
[C0\\left SB,name=leftSB
]
]
]
[IP
[{}
[XP, terminus
[{IP (without I0, V0)\\middlefield}, name=middlefield
[SpecIP\\subject position, set me left, name=subject
]
[phrases inside\\the VP, name=insidevp
]
]
]
]
[I$'$
[VP, name=vp
[V$^0$, name=v0, terminus, no path
[{V0, I0\\right SB}, name=rightSB
]
]
]
[{}
[I$^0$, terminus
]
]
]
]
]
]
% these are the table borders at the top and at the bottom
\draw [thick]
(prefield.north west) -- (rightSB.north east)
(prefield.west |- insidevp.south) -- (rightSB.east |- insidevp.south);
\draw
% the horizontal line in the middle
% for reasons I do not understand, the middlefield node does not extend over the phrases
% inside VP' node.
%(middlefield.south west) -- (middlefield.south east)
(subject.north west) -- (insidevp.north east)
%vertical lines
(prefield.north east) -- (prefield.south east)
(middlefield.north west)   -- (subject.south west)
(middlefield.north east)   -- (insidevp.south east)
%
(vp.south) -- (v0.center -| insidevp.west) -- (v0.west)
(v0.east) -- +(2.5pt,0) -- (vp.south)
;
\end{forest}
\caption{\label{Abb-GB-Topo}CP, IP and VP and the topological model of German}
\end{figure}

\end{document}


• Just a thought: could you build the table out of forest nodes too? – ajeh Nov 21 '14 at 19:44
• Just putting the table (tabular) inside the forest environment? I will try. – Stefan Müller Nov 21 '14 at 20:18
• I meant, you could put the text from the table cells inside the forest nodes, that are children of the last node in your tree, ex: 'SpecCM prefield' can be a child of the first 'XP', with the only difference that 'SpecCM prefield' would have a box around itself and no edge from 'XP'. – ajeh Nov 21 '14 at 22:30
• @StefanMüller It would be nice if you followed up on some of these questions. You have the wrong definition of terminus in your edited code. The code would work if you simply corrected this. – cfr Dec 17 '14 at 22:44

# EDIT

Here is a version which adds back the vertical rules, which I'd originally omitted (see below), since these are evidently required in this case.

\documentclass{article}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top}},
terminus/.style={tier=word, for children={tier=tabular}, for tree={fit=band}, for descendants={no path, align=left, l sep=0pt}},
empty nodes/.style={
delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}},
no path/.style={edge path={}},
set me left/.style={calign with current edge, child anchor=north west, for parent={parent anchor=south west}},
}
\usetikzlibrary{calc}
\begin{document}

\begin{figure}[htbp]
\centering
\begin{forest}
sn edges,empty nodes
[CP
[{}
[XP,terminus
[SpecCP\\prefield, name=p1
]
]
]
[C$'$
[{}
[C$^0$, terminus
[C0\\left SB, name=c0
]
]
]
[IP
[{}
[XP, terminus
[{IP (without I0, V0)\\middlefield}
[SpecIP\\subject position, set me left, name=specip
]
[phrases inside\\the VP, name=p3
]
]
]
]
[I$'$
[VP, name=vp
[V$^0$, name=v0, terminus, no path, anchor=east
[{V0, I0\\right SB}, name=p2, set me left
]
]
]
[{}
[I$^0$, terminus, name=io
]
]
]
]
]
]
\draw [thick]
(p1.north west) rectangle (io.east |- p3.south);
\draw
($(c0.north east)!1/2!(specip.west |- c0.north east)$) coordinate (p6) -- (p6 |- p3.south)
($(p1.north east)!1/2!(c0.north west)$) coordinate (p4) -- (p3.south -| p4)
($(specip.north east)!1/2!(p3.north west)$) coordinate (p5) -- (p3.south -| p5)
($(p2.north west)!1/2!(p2.north west -| p3.east)$) coordinate (p7) -- (p3.south -| p7)
(p6 |- p2.south) -- (p2.south -| p7)
(vp.south) -- (v0.center -| p3.west) -- (v0.west)
(v0.east) -- +(2.5pt,0) -- (vp.south)
;
\end{forest}
\caption{\label{Abb-GB-Topo}CP, IP and VP and the topological model of German}
\end{figure}

\end{document}


calc is being used to calculate the midpoint between two points. For example, ($(a)!1/2!(b)$) is the midpoint between (a) and (b). (a |- b) means the point which is vertically aligned with (a) and horizontally aligned with (b). The actual calculations used are just combinations of those two ideas, combined with the use of name= to name the various nodes in the tree.

# Initial Solution

Since it is considered typographically abhorrent to include vertical rules in tables, I initially omitted these. Since it might be useful to others in other cases where the rules do not themselves have meaning, here is the original solution. [If you wish to follow booktabs recommendations more precisely, you can adjust the rule widths ;).]

\documentclass{article}
\usepackage{forest}
\forestset{
sn edges/.style={for tree={parent anchor=south, child anchor=north,align=center,base=top}},
terminus/.style={tier=word, for tree={fit=band}, for descendants={no path, align=left, l sep=0pt}},
empty nodes/.style={
delay={where content={}{shape=coordinate,for parent={for children={anchor=north}}}{}}},
no path/.style={edge path={}},
set me left/.style={calign with current edge, child anchor=north west, for parent={parent anchor=south west}},
}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{forest}
sn edges,empty nodes
[CP
[{}
[XP,terminus
[SpecCP\\prefield, name=p1
]
]
]
[C$'$
[{}
[C$^0$, terminus
[C0\\left SB
]
]
]
[IP
[{}
[XP, terminus
[{IP (without I0, V0)\\middlefield}
[SpecIP\\subject position, set me left
]
[phrases inside\\the VP, name=p3
]
]
]
]
[I$'$
[VP, name=vp
[V$^0$, name=v0, terminus, no path
[{V0, I0\\right SB}, name=p2
]
]
]
[{}
[I$^0$, terminus
]
]
]
]
]
]
\draw [thick]
(p1.north west) -- (p2.north east)
(p1.west |- p3.south) -- (p2.east |- p3.south);
\draw
(p1.south west) -- (p2.south east)
(vp.south) -- (v0.center -| p3.west) -- (v0.west)
(v0.east) -- +(2.5pt,0) -- (vp.south)
;
\end{forest}
\caption{\label{Abb-GB-Topo}CP, IP and VP and the topological model of German}
\end{figure}

\end{document}


• Thanks a lot! A very cool solution! And I learned a new word (abhorrent). I was ready to use this solution, but then discovered that I need the vertical lines, since they have meaning. They correspond to areas in the sentence (prefield, left sentence bracket, middle field, right sentence bracket). The middle field is divided into two parts: the subject and the other phrases in the VP. This was depicted by the vertical lines. – Stefan Müller Nov 22 '14 at 10:30
• @StefanMüller Please see edit. I didn't realise the verticals had meaning in this case. (I've left the original solution below the edit as it might be useful to others where the vertical rules don't themselves carry meaning.) – cfr Nov 22 '14 at 13:58
• Thanks again! Could you please explain what these computations do? I cannot understand them. The box containing right SB has to be below V0 and I0. Is there a way to shift it to the right and maybe extend it to the right? – Stefan Müller Nov 22 '14 at 18:39
• @StefanMüller Please see updated code. – cfr Nov 22 '14 at 19:51
• @StefanMüller Further tweaking... bit hackish. – cfr Nov 22 '14 at 20:04