[Tex/LaTex] An underline with texts one both ends of a line

formatting

I want to make a blank between texts on both ends of a line. For example,a blank between texts on both ends of a line

\begin{enumerate}
\item There \hrulefill\ in the room.
\end{enumerate}

But, the above appears in student's version only, and I want to make a teacher's version where the answer is also printed in the blank. I don't have any idea but

\begin{enumerate}
\item There \underline{\makebox[100pt][l]{is a chair}} in the room.
\end{enumerate}

The method is not satisfactory because the length of the blank is unknown in advance.

My problems:

  1. How to put text on an \hrulefill?

  2. (A bonus question) I found out that the vertical position of the horizontal rule generated by \hrulefill and \underline is different. How can I control the vertical position of these horizontal rules?

Best Answer

While cslstr's method is more robust, one way to do this is to replace the \hrulefill with one that uses underlines using \xleaders.

\documentclass[letter,10pt]{article}
\newcommand{\ulfrule}{\xleaders\hbox{\underline{ }}\hfill\kern0pt}
\begin{document}
\begin{enumerate}
\item There \hrulefill{} in the room.
\item There \ulfrule{} in the room.
\item There \ulfrule{}\underline{is a chair}\ulfrule{} in the room.
\end{enumerate}
\end{document}

Which produces: example output

If you wanted the answer left aligned you can remove the beginning \ulfrule{} before the answer:

\documentclass[letter,10pt]{article}
\newcommand{\ulfrule}{\xleaders\hbox{\underline{ }}\hfill\kern0pt}
\begin{document}
\begin{enumerate}
\item There \hrulefill{} in the room.
\item There \ulfrule{} in the room.
\item There \underline{is a chair}\ulfrule{} in the room.
\end{enumerate}
\end{document}

More on what \xleaders does can be found in this answer