# Variable width box that either stretches to remaining width or is capped off

Is it possible to construct a box whose maximum width is specified, but in situations where that width is wider than the remaining width in a line, then the box just uses whatever space is left?

My use case is a book with fill-in-the-blanks. Each fill-in-the-blank has a width attached to it, like say 15em. It's great if the fill-in-the-blank is printed as a 15em wide box. But sometimes the fill-in-the-blank comes near the end of the line, and runs into the margin. I'd prefer it to just terminate as a shorter box. (And I'd prefer this over any kind of wrapping.)

With my use case in mind, here is something like what I am after in a sort of pseudo syntax: \framebox[min(15em, width of an hfill)]{\strut}.

One way would be to use \leaders.

\documentclass{article}

\def\zz{\leavevmode\vrule height 10pt\nobreak\leaders\vbox{\vskip-.4pt\hrule width .4pt\vskip 10pt \hrule\vskip-.4pt} \hskip 15em minus 15em \nobreak\hbox{\vrule height 10pt}}

\begin{document}

x\hspace{15em}X
zzzzzzzzzz \zz\   zzzzzzzzzzz \zz\ zzzzzzzzzzzzzz \zz\
zzzzzzzzzz \zz\   zzzzzzzzzzz \zz\ zzzzzzzzzzzzzz \zz

aaaa aaaaaaaaaaa aaaaaaaaaaaaaaaaa\zz

ZZZZZZZZZ aaaa aaaaaaaaaaa aaaaaaaaaaaaaaaaa\zz

ZZZZZZZZZZZZZZ aaaa aaaaaaaaaaa aaaaaaaaaaaaaaaaa\zz

ZZZZZZZZZZZZZZZZZZ aaaa aaaaaaaaaaa aaaaaaaaaaaaaaaaa\zz

\end{document}


You could use the linegoal pack­age which pro­vides the \line­goal pseudo length expanding to the re­main­ing length of the line.