[Tex/LaTex] labeling underlined text over multiple lines

highlightingline-breakingsoul

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}

Best Answer

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}

soulpos and numbered underlines

Related Question