[Tex/LaTex] Breakable fill-in blanks

line-breakingplain-textext-decorations

What is a good way to create blank space (preferably, underlined — it is to be filled in by hand when document is printed) that takes up all the remaining space on a line. The blank space may appear at the beginning, in the middle or at the end of the line; it may also occupy the whole line.

I would appreciate a solution that survives changing the font and font size of surrounding text, as well as the size of page and page margins.

UPDATE: I am looking for a solution with Knuth's TeX and plain TeX.

UPDATE 2: I find egreg's suggestion that blanks have a minimum length a good one. However, in this case it would be very convenient if blanks can be broken across lines (automatically, of course) in cases where the blank is too long to fit on current line and has to be broken up either because it is longer than the line or because text on that line is too short and would result in an underfull box if left alone.

UPDATE 3:

Types of fill-in blanks I need:

1) unbreakable blanks of a specified fixed length; (egreg already provided an elegant solution to this case here)

2) breakable blanks of a specified fixed length;

3) breakable blanks no shorter than a specified minimum length. These are the same as type 2 blanks except that the last part of a broken blank (or the whole blank when it is not broken) should expand and take up all the available space on its line (like \hrulefill).

Best Answer

If you need to break the underlined space with given size to two parts at the end of the line (sum of the parts is approximately the given size) then there is a solution:

\newcount\tmpnum \newdimen\tmpdim

\def\uelement{\hbox{\vrule height-1pt depth1.4pt width5pt}\penalty0 
    \hskip0pt minus.1pt \relax}
\def\softelement{\leaders\vrule height-1pt depth1.4pt\hskip 0pt plus5pt\relax}

\def\underlined#1{\bgroup
  \tmpdim=#1 \divide\tmpdim by327680 \tmpnum=\tmpdim
  \leavevmode \softelement
  \loop \ifnum\tmpnum>0\advance\tmpnum by-1 \uelement \repeat
  \softelement \egroup
}

First \underlined{5cm} second.

First \underlined{20cm} second.

\bye

broken underlined space

The code above implements the type 2 of the breakable blanks, as mentoned in the Update 3 of the question. The type 3 can be simply implemented by adding the \ufill macro:

\def\ufill{\leaders\vrule height-1pt depth1.4pt \hfill}.

First \underlined{5cm}\ufill second.

First \underlined{20cm}\ufill second.