# Upside-down syntax trees for linguistics with horizontal lines

I know that syntax analysis can be done in Latex using using TikZ, but the tree-structure that produces isn't really used in my country. Here, we mostly use straight lines instead of hierarchy trees.

Here's an example what I want to achieve. I don't know where to start from.

Is there any (more or less) easy way to do it? Any idea?

## Edit:

Even though all solutions provided until the moment are right, it would be nice to modify the following characteristics of the different diagrams you've proposed:

• The sentence that is being analysed ("La novela que me ha regalado mi hermana...") should be on top of the graphic and all words must be in the same line; "la" can't be immediately over "Det" and "está" shouldn't be immediately over "N/V".

• All the syntax functions of the differents words ("OP", "S/SN", "PN/SV", "Det"...) should be centered with the respective lines they have above (or at least near to the center but without having to change manually the spacing).

• It should be possible to modify the height of the diagram.

-
Could you please supply some code? I realise you don't know how to draw the diagram but at least provide the document framework and the text which needs to go into the diagram. This makes it a lot easier to try out a solution than trying to start from scratch. –  cfr Apr 19 '14 at 21:43
–  cfr Apr 19 '14 at 21:54
Out of curiosity, were you able to figure out solutions to the three items under your “Edit” section? –  hftf May 2 '14 at 19:37
Not really, but your answer fits almost what I want to do. However, it would be nice to solve them. –  JnxF May 2 '14 at 20:51

Here’s a solution that uses the excellent forest package.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}

% Node shape adapted from http://www.texample.net/tikz/examples/data-flow-diagram/
\makeatletter \pgfdeclareshape{myunderline}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\foreach \from in
{center,base,north,north east,east,south east,south,south west,west,north west}{
\inheritanchor[from=rectangle]{\from}
}
\backgroundpath{
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% This can be improved by removing magic numbers
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya+1.75em}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya+1.75em}}
}
} \makeatother

\begin{document}
\begin{forest}
for tree={
fit=band, % Isolates space above this node from siblings’ descendants
no edge,
% Uncomment the line below for the dotted edges
% edge={dotted, semithick, gray!50, shorten <=8pt}, parent anchor=north,
% This can be improved by reducing space between levels where edges are drawn
inner sep=0pt, outer sep=0pt,
l sep=0pt, s sep=6pt, text depth=0.5em, grow'=north,
where level=0{} % No style for dummy root node
{where n children=0
{font=\bfseries,tier=word} % Leaves in bold on the same tier
{font=\small,tikz={\node[draw, thick, myunderline, fit to tree] {};}} % Non-leaves
}
}
% This can be improved by removing the need for a parent and sibling of the actual root
[,phantom[,phantom][OP
[S/SN
[Det [La] ]
[N/Sust [novela] ]
[PV/SV,
[\textit{nexo} [que] ]
[CI/SN [me] ]
]
[S/SN
[Det [mi] ]
[N [hermana] ]
]
]
]
[PN/SV
[N/V [est\'a] ]
]
[CCL/SPrep
[E [en] ]
[T/SN
[N [Australia\rlap.] ]
]
]
]
]]
\end{forest}
\end{document}


And here’s another version with faint dotted edges:

You can render the same structure in a more conventional appearance just by changing options:

for tree={
edge={dotted, semithick, gray!80, shorten <=1pt,shorten >=3pt},
parent anchor=south, child anchor=north,
inner sep=0pt, outer ysep=2pt,
text depth=0.5em,
where n children=0{font=\bfseries,tier=word}{font=\small}
}


So you can see why one might prefer using forest instead of bussproofs or semantics. Also, the forest tree syntax is much simpler, and is not “backwards” as seen in cfr’s answer.

Take a look at the forest manual for more style options.

-
Nice solution. BTW, how can I reduce the vertical height of the diagram? I've tried l sep with negative values without success. Thank you –  JnxF Apr 20 '14 at 14:39
Very slick solution. I wish I understood forest. I don't see why my solution is 'backwards' though. You are analysing the sentence so that's where you are starting, surely? In that sense, your solution seems to be 'backwards' if anything. Still very nice, though - especially being able to easily turn it upside down like that. –  cfr Apr 20 '14 at 17:10
@JnxF I also tried playing with l sep, inner ysep, outer ysep, and the like with no success, but I thought my solution answered your question enough to still post it. You could post a new question, or just wait for other people who know more about forest to stop by. –  hftf Apr 20 '14 at 20:16
@cfr I guess it depends on your perspective :). In my experience, trees are usually specified parent-first — that’s how the syntax (heh) of most of the various tree packages I’ve tried (forest, tikz-qtree, qtree, synttree, “plain” TikZ) work. It would be trivial to replace forest with any of those if you wanted to, but harder to switch to bussproofs or semantics since they are specified child-first. –  hftf Apr 20 '14 at 20:29
@htft I don't considerate necessary to ask another question. I've just edited this one. –  JnxF Apr 20 '14 at 23:13

For example:

\documentclass{standalone}
\usepackage[inference]{semantic}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\begin{document}

\setpremisesend{0pt}
\setpremisesspace{1pt}
\setnamespace{0pt}
\inference{%
\inference{%
\inference{\mbox{La}}{Det}
&
\inference{\mbox{novela}}{N/Sust}
&
\inference{%
\inference{%
\inference{\mbox{que}}{nexo}
&
\inference{\mbox{me}}{CI/SN}
&
}
{PV/SV}
&
\inference{%
\inference{\mbox{mi}}{Det}
&
\inference{\mbox{hermana}}{N}
}
{S/SN}
}
}
{S/SN}
&
\inference{%
\inference{\mbox{está}}{N/V}
&
\inference{%
}
&
\inference{%
\inference{\mbox{en}}{E}
&
\inference{%
\inference{\mbox{Australia}}{N}
}
{T/SN}
}
{CCL/SPrep}
}
{PN/SV}
}
{OP}

\end{document}


-
I have absolutely no idea what exactly the OP wants, or how semantic works, but it seems to me a key part is that the lines are the same length as the word(s) they refer to, and yours don't seem to have that. Is that adjustable? –  jlv Apr 20 '14 at 0:30
I mean the words in the sentence above the picture, for example "nexo" refers to "que", "CI/SN" to "me", "N/V" to "ha regalado" and "PV/SV" to "que me ha regalado". You haven't included the sentence in your answer, even though the lines more or less line up with the words they refer to. I'm just under the impression that the lines being the same length as the words is key, since this is a linguistics question and the words below seem to describe parts of the sentence above. –  jlv Apr 20 '14 at 1:06
@JānisL Yes. Sorry. I deleted my comment because I realised what you meant. I am not sure you can (straightforwardly) do that with semantic but you can put the words right above the relevant analysis (see edit) so that it is obvious what goes with what. This is the way semantic is used on the LaTeX for Linguists site and this option looked like the closest match to the image the OP posted. Another possibility would be to adapt a package intended for typesetting similar kinds of proofs in formal logic. (bussproofs is maybe the best known.) But this seemed a little more promising here... –  cfr Apr 20 '14 at 1:22

Here's a different way... based on my answer at Highlighting text through stacked colored underlines

\documentclass{article}
\usepackage{stackengine}
\newlength\tmpln
\newlength\lunderset
\newlength\rulethick
\lunderset=1\baselineskip\relax
\rulethick=.8pt\relax
\def\stackalignment{l}
\newcommand\nunderline[3][1]{\setbox0=\hbox{#2}\tmpln=\wd0%
\stackunder[#1\lunderset-\rulethick]{\strut#2}{%
\smash{\raisebox{-.6\baselineskip}{\makebox[0pt][l]{\scriptsize #3}}}%
\rule{\tmpln}{\rulethick}}}%
\let\Nun\nunderline
\let\HS\hspace
\begin{document}
\Nun{\Nun{\Nun[3]{La}{Det}}{}}{}%
\Nun{\Nun[4]{ }{}}{}%
\Nun{\Nun{\Nun[3]{novela}{N/Sust}}{}}{}%
\Nun{\Nun[4]{ }{}}{}%
\Nun{\Nun{\Nun{\Nun{\Nun   {que}{\itshape nexo}}{}}{}}{}}{}%
\Nun{\Nun{\Nun{\Nun[2]{ }{}}{}}{}}{}%
\Nun{\Nun{\Nun{\Nun{\Nun   {me}{CI/SN}}{}}{}}{}}{}%
\Nun{\Nun{\Nun{\Nun{\Nun   {ha}{}}{}}{}}{}}{}%
\Nun{\Nun{\Nun{\Nun{\Nun   { }{}}{}}{}}{}}{}%
\Nun{\Nun{\Nun[3]{ }{}}{}}{}%
\Nun{\Nun{\Nun{\Nun{\Nun   {mi}{Det}}{}}{}}{}}{}%
\Nun{\Nun{\Nun{\Nun[2]{ }{}}{}}{}}{}%
\Nun{\Nun{\Nun{\Nun{\Nun   {hermana}{\HS{5ex}N}}{S/SN}}{}}{}}{OP}%
\Nun[5]{ }{}%
\Nun{\Nun{\Nun[3]{est\'a}{N/V}}{}}{}%
\Nun{\Nun[4]{ }{}}{}%