# TikZ: path-style syntax upside-down tree for adpositional grammars

I would like to reproduce in TikZ the following adpositional path-like adpositional tree, that I am able to draw in full only by hand. See here:

First MWE: my minimal working example (MWE) using tikz-dependency succeed only to produce the text, not the paths, as arrows always points at cells. This is perfectly fine for dependency trees and that package is great for this purpose, but I need something different here.

Any alternative idea is welcome, not necessarily with this package.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-dependency}

\begin{document}

\begin{dependency}
\begin{deptext}[column sep=1em]
the \& girl \& for \& wh- \& om \& the \& man \& bought \& the \& book\\
A \& O \& U \& U \& O \& A \& O \& I \& A \& O\\
\end{deptext}
\end{dependency}

\end{document}


I do not know how to draw such paths with tex-dependency, only arrows that points to words.

Second MWE I tried to use forest then, but I am not able to produce a satisfying result. Here the code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage[linguistics]{forest}

\begin{document}

\bracketset{action character=@}
\newcount\xcount
\edef\xtemp{[$\noexpand\times_{\the\xcount}$[#1]]}%
\expandafter\bracketResume\xtemp
}
\begin{forest}
phantom,
delay={where level=1{content={\strut #1}}{}}
@+
[
\x{the [A[.]] }
\x{girl [O[.]] }
\x{for [U[.]] }
\x{wh- [U[.]] }
\x{om [O[.]] }
\x{the  [A[.]] }
\x{man [O[.]] }
\x{bought [I[.]] }
\x{the [A[.]] }
\x{book [O[.]]}]
\end{forest}

\end{document}


This is the output of the second MWE:

I think paths should be identified with three parameters:

1. starting point -- e.g. start the path below 'girl' and 'O';
2. depth -- e.g. go 6 in the path below 'girl' and 'O';
3. arrow direction; this can be:
• none, e.g. the path below 'girl' and 'O';
• right, e.g. go 1 right in the path below the 'the' on the left of 'girl';
• left, e.g. go 4 left in the path below 'bought' and 'I';

If arrow direction is left or right, next to the arrow head there should be an extra parameter for the final grammar character of the path such as in the figure, which probably would be something like \overleftarrow{O} for the path starting under 'girl' and 'O' and \overrightarrow{A} for the path starting under 'bought' and 'I';

The last features are the "bridges" (I do not have a better name) on depth 1 (right-side bridge) and 4 (left-side bridge) for the path starting under the 'wh' and 'U'.

Any suggestion on any TikZ-based package is more than welcome.

• I noticed that you must have posted while I was writing the comment. I will delete the comment. – A.Ellett Sep 26 '18 at 20:08

This is certainly not yet a full answer, just to tell you that I do not agree with your scepticism on the package. IMHO this is a package with an extremely well-written manual, and here is why I believe you can achieve what you want with it. The key ingredient of this answer is a macro

\Connect{<start>}{<end>}{<level>}{<label>}


where start, end and level are all integers. Its use is illustrated by the following MWE.

\documentclass[border=3.14mm,standalone]{standalone}
\usepackage{mathtools}
\usepackage{tikz-dependency}
\usetikzlibrary{intersections} % not used ... YET
\newcounter{depaths}
\newcommand{\Connect}[5][]{
\stepcounter{depaths}
\draw[name path=dep connect \thedepaths](\wordref{2}{#2}) -- ++ (0,-#4) coordinate (aux-\thedepaths-1)  -|
(\wordref{2}{#3}) coordinate[pos=0.5] (aux-\thedepaths-2);
\draw[-latex,shorten >=1pt] (aux-\thedepaths-1) -- (aux-\thedepaths-2);
\ifnum#3>#2
\node[anchor=south east] at (aux-\thedepaths-2) {#5};
\else
\node[anchor=south west] at (aux-\thedepaths-2) {#5};
\fi
\foreach \X in {1,...,#4}
{\fill (\wordref{2}{#2}) ++ (0,-\X) circle(1.5pt);
\fill (\wordref{2}{#3}) ++ (0,-\X) circle(1.5pt);}
\pgfmathtruncatemacro{\DeltaX}{abs(#3-#2)}
\ifnum\DeltaX>1
\foreach \X in {#2,...,#3}
\fill (\wordref{2}{\X}) ++ (0,-#4) circle(1.5pt);
\fi
}
\begin{document}
\begin{dependency}
\begin{deptext}[column sep=1em]
the \& girl \& for \& wh- \& om \& the \& man \& bought \& the \& book\\
A \& O \& U \& U \& O \& A \& O \& I \& A \& O\\
\end{deptext}
\Connect{1}{2}{1}{$\overleftarrow{O}$}
\Connect{4}{2}{6}{$\overleftarrow{O}$}
\Connect{4}{3}{1}{$\overleftarrow{O}$}
\end{dependency}

\end{document}


ADDENDUM: This is an experimental version that draws arcs over intersections. As you can see, there are quite a number of \typeouts and this is by far not perfect, but perhaps a start. To be more precise, the arcs won't be drawn whenever one of the following criteria is met:

1. The first path starts where the second path starts or ends.
2. The first path ends where the second path starts or ends.
3. The paths have the same level (=3rd parameter of the macro \Connect{4}{3}{1}).

It is easy to see that if any of those criteria are met, there is a great chance that there shouldn't be an arc, but the following two paths won't have an arc either:

******************
|   | |
*---*-*
| |
*-*


It is possible, though cumbersome, to close this loophole also. Depending on whether this will really also get used somewhere, I may pick up enough motivation to do that and also to make the macros more TikZy, i.e. work with pgfkeys.

\documentclass[border=3.14mm,standalone]{standalone}
\usepackage{mathtools}
\usepackage{tikz-dependency}
\usetikzlibrary{intersections}
\newcounter{depaths}
\newif\ifIntersect
\tikzset{every picture/.append style={execute at begin picture={%
\xdef\LstPaths{}% may be unnecessary
\setcounter{depaths}{0}}}}
\newcommand{\Connect}[5][]{
\stepcounter{depaths}%\typeout{new\space path\space\thedepaths}
\draw[name path=dep connect \thedepaths-tmp](\wordref{2}{#2}) -- ++ (0,-#4) coordinate (aux-\thedepaths-1)  -|
(\wordref{2}{#3}) coordinate[pos=0.5] (aux-\thedepaths-2);
\draw[-latex,shorten >=1pt] (aux-\thedepaths-1) -- (aux-\thedepaths-2);
\ifnum#3>#2
\node[anchor=south east] at (aux-\thedepaths-2) {#5};
\else
\node[anchor=south west] at (aux-\thedepaths-2) {#5};
\fi
\foreach \X in {1,...,#4}
{\fill (\wordref{2}{#2}) ++ (0,-\X) circle(1.5pt);
\fill (\wordref{2}{#3}) ++ (0,-\X) circle(1.5pt);}
\pgfmathtruncatemacro{\DeltaX}{abs(#3-#2)}
\ifnum\DeltaX>1
\foreach \X in {#2,...,#3}
\fill (\wordref{2}{\X}) ++ (0,-#4) circle(1.5pt);
\fi
\ifnum\thedepaths>1
\foreach \Y [count=\X] in \LstPaths
{\Intersectfalse
%\typeout{checking\space intersection\space with\space path\space \X}
\path[name intersections={of=dep connect \thedepaths-tmp and dep
connect \X-tmp,total=\t}] \pgfextra{%\typeout{\t:\Y}
\ifnum\t>0
\global\Intersecttrue
%\typeout{paths\space\X\space and\space\thedepaths \space\space have\space common\space points}
\fi};
\foreach \V [count=\W] in \Y
{\ifcase\W % no 0
\or % 1 boring
\or % 2
\ifnum\V=#2
%\typeout{case2-2}
\global\Intersectfalse
\fi
\ifnum\V=#3
%\typeout{case2-3}
\global\Intersectfalse
\fi
\or
\ifnum\V=#3
%\typeout{case3-3}
\global\Intersectfalse
\fi
\ifnum\V=#2
%\typeout{case3-2}
\global\Intersectfalse
\fi
\or
\ifnum\V=#4
%\typeout{case4}
\global\Intersectfalse
\fi
\fi}
\ifIntersect
\node[circle,fill=white,outer sep=0pt,minimum size=10pt] (aux-int) at (intersection-1){};
\draw (aux-int.south) -- (aux-int.north);
\draw[white,double=black] (aux-int.west) to[out=60,in=120] (aux-int.east);
%\typeout{real\space intersection\space of\space path\space \thedepaths\space with\space path\space \X}
\fi
}
\fi
\ifnum\thedepaths=1
\xdef\LstPaths{{\thedepaths,#2,#3,#4}}
\else
\xdef\LstPaths{\LstPaths,{\thedepaths,#2,#3,#4}}
\fi
}
\begin{document}
\begin{dependency}
\begin{deptext}[column sep=1em]
the \& marmot \& for \& wh- \& om \& the \& duck \& bought \& the \& cake\\
A \& O \& U \& U \& O \& A \& O \& I \& A \& O\\
\end{deptext}
\Connect{1}{2}{1}{$\overleftarrow{O}$}
\Connect{4}{2}{6}{$\overleftarrow{O}$}
\Connect{3}{8}{4}{$\overleftarrow{3}$}
\Connect{5}{3}{1}{$\overleftarrow{O}$}
\end{dependency}
\end{document}


• you were faster than my comment! – Federico Gobbo Sep 26 '18 at 20:26
• The result is already impressive. No, you are not missing anything! Intersections are the only things missing. I've seen an example here texample.net/media/tikz/examples/TEX/line-junction.tex and I will try to understand how it works. Thank you a lot again! – Federico Gobbo Sep 26 '18 at 20:45
• +1 for the conditionals... – J Leon V. Sep 26 '18 at 22:42
• @JLeonV. Thanks! Actually this is a real mess since the paths technically intersect but are not considered intersecting as they "only" merge. I actually feel very bad, given that the starting point is a really nice package where everything is fully customizable in pgfkeys, and I add the code of a wood chopper (or wood chuck? ;-) ... – user121799 Sep 26 '18 at 22:45
• @marmot, yes but there are structures in your code that I did not know what could be done ... and well, it is useful to apply in other drawings, of course referring to this self-denying marmot... – J Leon V. Sep 26 '18 at 22:54