# How to annotate text with hierarchical overlapping underbracket notes

Picture below shows some text that is annotated in underbrackets with notes "note1", "note2", and "note3". How to do this in latex?

• Is that supposed to be anything like "sentence diagramming"? tex.stackexchange.com/questions/183032/sentence-diagramming And if not, what are the "rules" that need to be followed? – Steven B. Segletes Apr 12 '16 at 1:58
• No that looks way too complicated. The only rules are these 1) sentence is on one line. 2) Underbrackets are hierarchically nested in an overlapping manner to put notes on sequences of words as shown in the picture above – caveman Apr 12 '16 at 2:24

This is the more workable of my two solutions below, but it uses a colored line rather than an underbracket. It is based on my answer at Highlighting text through stacked colored underlines.

\documentclass{article}
\usepackage{stackengine}
\usepackage{xcolor}
\newlength\lunderset
\newlength\rulethick
\lunderset=8.5pt\relax
\rulethick=.8pt\relax
\def\stackalignment{l}
\newcommand\nunderline[3][1]{\setbox0=\hbox{#2}%
\stackunder[#1\lunderset-\rulethick]{\strut#2}{\color{#3}\rule{\wd0}{\rulethick}}}
\newcommand\nundertext[2][1]{\def\useanchorwidth{T}\def\stackalignment{c}\smash{%
\stackunder[#1\lunderset-\rulethick+.6\lunderset]{}{%
\scriptsize\strut#2}}}
\begin{document}
\nunderline{This i\nundertext{Note 3}s }{red}%
\nunderline{\nunderline{some}{red}}{blue}%
\nunderline[2]{ text th\nundertext[2]{Note 2}at I am }{blue}%
\nunderline{\nunderline[2]{writin\nundertext[3]{Note 1}g}{blue}}{cyan}%
\nunderline[3]{ now.}{cyan}
\end{document}


This approach may or may not be easier to execute. It is based on my answers at How to link two terms in math mode and Gathering underbraces

\documentclass{article}
\usepackage{stackengine}
\def\textscrm#1{\textrm{\scriptsize#1}}
\stackMath
\def\rldp{1.3ex}
\def\rlht{.8ex}
\def\rlwd{.8pt}
\setstackgap{L}{\rldp}
\def\uvbarR#1{%
\def\stackalignment{r}\def\stacktype{S}\stackunder[-\rlwd]{%
\def\stackalignment{c}\def\stacktype{L}\stackunder{#1}{\rule{\rlwd}{\rlht}}%
}{\setbox0\hbox{$#1$}\rule{.5\wd0}{\rlwd}}%
}
\def\uvbarL#1{%
\def\stackalignment{l}\def\stacktype{S}\stackunder[-\rlwd]{%
\def\stackalignment{c}\def\stacktype{L}\stackunder{#1}{\rule{\rlwd}{\rlht}}%
}{\setbox0\hbox{$#1$}\rule{.5\wd0}{\rlwd}}%
}
\def\uvbar#1#2{%
\def\stacktype{S}\def\stackalignment{c}\def\useanchorwidth{T}\stackunder[0pt]{%
\def\stacktype{L}\setbox0\hbox{${}#1{}$}%
\stackunder[\rldp]{{}#1{}}{\rule{\wd0}{\rlwd}}%
}{\def\stacktype{S}\stackunder[1pt]{\rule{\rlwd}{\rlht}}{\scriptstyle#2}}%
}
\newcommand\ubar[2]{\uvbarR{\textrm{}}\uvbar{\textrm{#1}}{\textscrm{#2}}%
\uvbarL{\textrm{}}}
\newcommand\phubar[2]{\uvbarR{\textrm{}}\uvbar{\phantom{\textrm{#1}}}{\textscrm{#2}}%
\uvbarL{\textrm{}}}
\begin{document}
\par\ubar{This is some}{Note 3} text that I am writing now.
\par\vspace{-.6\baselineskip}\leavevmode%
\phantom{This is }\phubar{some text that I am writing}{Note 2}%
\phantom{ now.}
\par\vspace{-.6\baselineskip}\leavevmode%
\phantom{This is some text that I am }\phubar{writing now}{Note 1}%
\phantom{.}
\end{document}


When we are creating macros then user comfortability may be important too. So, my macros define \unotedef where parameters of "underlined note" is declared and then \unote uses declared data in real text.

\def\unotecs#1{\csname unote:#1\endcsname}
\def\unote#1{\leavevmode\hbox to0pt{\vtop{\null \kern-2.5ex \kern\unotecs{#1A}%
\hbox{$\underbrace{\hskip\unotecs{#1B}}_{\hbox{\unotecs{#1C}}}$}}\hss}}
\def\unotedef#1#2#3#4{%
\expandafter\def\csname unote:#1A\endcsname{#4}%
\setbox0=\hbox{#2}\expandafter\edef\csname unote:#1B\endcsname{\the\wd0}%
\expandafter\def\csname unote:#1C\endcsname{#3}%
}
\unotedef{1} {This is some}{note 3}{0ex}
\unotedef{2} {some text that I am writing}{note 2}{4ex}
\unotedef{3} {writing now}{note 3}{6ex}

\unote{1}This is \unote{2}some text that I am \unote{3}writing now.