# labeling underlined text over multiple lines

I have a large number of documents I need to create, and there is one very particular need I cannot find a solution for.

In these documents, there are pieces of text that are underlined and they must be labelled with a number or letter, placed in the middle of the text immediately below the underline.

To do this for short pieces of text, I was using the \underset in math mode with the \ul command from the soul package to do what I needed. This only worked if the items were short and did not go over line breaks.

I know the \ul command by itself handles line breaks well, but I cannot think of how to get this centered label to work across multiple lines. I need the label to appear below the center of the text on each line of the underlined portion.

For example, if I have an underlined sentences labeled "4" that is three lines on paper, it must have the number "4" appearing below the underlined portion on each of the three lines on the page. In particular, if the underlined portion only has a small piece, such as a single word, on one of the lines it appears in, then the label must be centered below that underlined portion, not centered under the whole line in which it appears.

I know this is a complicated request, but I hope one of you brilliant folks has a solution for me to do this in latex (or context if there is only a way to do it there).

Any suggestions are appreciated.

\documentclass[two column]{article}
\usepackage{amsmath,soul}

\begin{document}

$\underset{4}{\text{\ul{This is short text}}}$

$\underset{5}{\text{\ul{Much longer text in this format will not split properly along lines, which is the effect I would like to produce.}}}$

\ul{But if I only use the ul command, I will not get the propery labelling on each line like I want}.

\bigskip

$\underset{6}{\text{\ul{I want something to look like this text does, but I}}}$ \\
$\underset{6}{\text{\ul{don't want to manually break lines.}}}$ More text here.

\end{document}

-
It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it and give them a starting point. – Peter Grill Feb 21 '12 at 23:00
I wish i could, but i have no idea where to begin on this. I don't have a working example on any level – grizzilus Feb 21 '12 at 23:04
A working example does not mean one that solves the problem, but rather one that compiles and illustrates the undesired result. It is easier to see the problem rather than try to figure out what you mean in your description. This will also ensure that the solution works for your intended situation. – Peter Grill Feb 22 '12 at 0:31
ok i added an example, i hope it summarizes what i said in the post – grizzilus Feb 22 '12 at 1:42

I've just released soulpos, which allows this with a different approach based on savepos (pdftex or compatible required). Two passes are required:

\documentclass[two column]{article}
\usepackage{amsmath,soul}
\usepackage{soulpos}

\ulposdef{\ulnumaux}{%
$\underset{\saveulnum}{\rule[-.7ex]{\ulwidth}{.4pt}}$}

\newcommand{\ulnum}[2]{%
\def\saveulnum{#1}%
\ulnumaux{#2}}

\begin{document}

\ulnum{4}{This is short text}

\ulnum{6}{I want something to look like this text does, but I
don't want to manually break lines.} More. \ulnum{7}{And more text here.}

\end{document}


-
wow, this is insanely helpful, exactly what i wanted. thanks for this amazing package! – grizzilus Feb 29 '12 at 8:01
Yes a +1 from me as well (even though you just stole my green "accept" tick:-) – David Carlisle Feb 29 '12 at 9:14
What's amazing is soul. – Javier Bezos Feb 29 '12 at 15:18

Far from perfect, but it's a start. This version doesn't work if start and end are on same line, and doesn't work if a page break happens between them. Both would be fixable.

I looked at this the other day but couldn't remember the trick to measure the position of a point along the line, then by coincidence @FrankMittelbach popped up and gave a full account of it here

How do you change indentation in the middle of a paragraph?

So here's that same code, re-arranged a bit to produce the image above (plain TeX in this version)

\hsize=.5\hsize
\newdimen\lastlinewidth

\baselineskip1.5\baselineskip

\def\ustart{%
\ustartend{%
\hbox to \hsize{\hskip\lastlinewidth
\leaders\hrule height 0pt depth 1pt\hskip 0pt plus 2fill
\kern-\hsize
\hskip\lastlinewidth\hfill\smash{\raise-7pt\hbox{\tempn}}\hfill
}}}

\def\uend{%
\ustartend{%
\count0=\prevgraf
\unskip\hbox to \hsize{%
\smash{\raise-\baselineskip\vbox{%
\loop
\ifnum\count0>2
\hbox to \hsize{%
\leaders\hrule height 0pt depth 1pt\hskip 0pt plus 2fill
\kern-\hsize
\hfill\smash{\raise-7pt\hbox{\tempn}}\hfill
}%
\repeat
\hbox to \lastlinewidth{%
\leaders\hrule height 0pt depth 1pt\hskip 0pt plus 2fill
\kern-\lastlinewidth
\hfill\smash{\raise-7pt\hbox{\tempn}}\hfill
}\hfill}}}}}

\def\ustartend#1#2{%
\def\tempn{#2}%
\leavevmode\hbox{}%
\begingroup
\predisplaypenalty=10000
\postdisplaypenalty=10000
\abovedisplayskip=-\baselineskip
\abovedisplayshortskip=-\baselineskip
\belowdisplayskip=0pt %-\baselineskip
\belowdisplayshortskip=0pt %-\baselineskip
$$\global\lastlinewidth\predisplaysize \global\advance\lastlinewidth by -2em #1%$$%
\endgroup
\vskip-\baselineskip
\vskip-\parskip
\noindent\kern\lastlinewidth
}

\def\n{One two thr\-ee four five six seven eight nine ten. }
\def\c{Red blue yellow green. }

\n\c\ustart{3}start underline \n\n
\n\n\n\n
\n\n end underline\uend{3} first text after end
\n\n

\n\n\n\n\c\ustart{4}start again\c\n\n\c\c\n
\n\n\n end underline\uend{4} first text after end
\n\c\c\n\n\c\c\n
\n\n\n\n\c\c\n\n\c\c\n

\bye

-
i need to go through this carefully, but this looks like i can definitely tweak this to work for what i need. thanks for the help! – grizzilus Feb 23 '12 at 0:59