# An underline with texts one both ends of a line

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

\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?

-
Do you always want an \hrulefill to represent the missing word(s)? What about breaks over lines? – Werner Mar 13 '14 at 3:24
Welcome to TeX.SX! A tip: You can use backticks  to mark your inline code as I did in my edit. – Adam Liter Mar 13 '14 at 3:35
Werner: I do not always want an \hrulefill. What are breaks over lines? – HYC Mar 13 '14 at 5:55
Welcome to TeX.SX :-) Consider the exam document class. – Sean Allred Mar 13 '14 at 6:37

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}
\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:

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

\documentclass[letter,10pt]{article}
\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

-
This is almost what I need. However, I hope that the answer be aligned left rather than center inside the blank, yet a little space say 0.5ex should be left at the beginning of the blank. Could you teach me how to obtain it? – HYC Mar 13 '14 at 12:25
I updated the answer to allow for left aligned answers. – Alex Grede Mar 13 '14 at 12:32

Here's a simple implementation:

\documentclass{article}

\newif\ifteacher % starts out false

\unskip\ \lhrulefill{#1}%
\ifteacher\makebox[0pt]{#2}\fi
\lhrulefill{#1}\ \ignorespaces}
\newcommand{\lhrulefill}[1]{%
\leavevmode
\hskip\glueexpr#1/2\relax\relax % how much it should extend
\kern0pt
}

\begin{document}

\begin{enumerate}
\item There \fillanswer{is a chair} in the room.

\item There \fillanswer[5cm]{is a chair} in the room.

\teachertrue
\item There \fillanswer{is a chair} in the room.

\item There \fillanswer[5cm]{is a chair} in the room.
\end{enumerate}

\end{document}


The default value for the optional argument is \fill, which means that essentially \hrulefill is executed (but the rule is lowered). Actually we set two rules and the zero width box in between them when \ifteacher is set to true.

-
I have the very same task as in third item of your list so adopted your solution. Suddenly the problem arise: I have small space right in the middle of the hrule, so it seems that it is actually two rules separated by a space. Would you please explain hot to get rid of this space (proof pic: postimg.org/image/xy0xfpftr the space is under letter в). – Artem Pelenitsyn Jul 28 '14 at 18:29
@ArtemPelenitsyn You're missing a % after \makebox[0pt]{#1} – egreg Jul 28 '14 at 19:08
that's great, thank's! – Artem Pelenitsyn Jul 28 '14 at 20:02

This method uses some redefinitions of the \uline macro from ulem to print out just the underlining from some text (with the text itself invisible). (See this answer for a similar solution.)

I added a bit of space on each side of the text so the blank is a bit larger than it would need to be (to allow for handwriting). As illustrated, this will also gracefully handle very long "blanks" that end up extending over multiple lines.

The Student Version is made by keeping the first \let\filltheblanks... command and commenting the second. The Teacher version comments the first command and keeps the second.

\documentclass[]{report}

\usepackage{enumerate}
\usepackage[normalem]{ulem}

\begin{document}
\LARGE % Size seemed appropriate for this type of assignment

% Fill-in-the-blank command

% For Student Version
\let\filltheblanks\phantom

% For Teacher Version
\let\filltheblanks\null

% Redefined \uline
\makeatletter
\def\UL@putbox{\ifx\UL@start\@empty \else % not inner
\vrule\@width\z@ \LA@penalty\@M
\filltheblanks{\box\UL@box}%
\fi}
\makeatother
%%%%%%%%%%%%%%%%

\begin{enumerate}[1.]

\item There \fillintheblank{was a dog} in the room.

\item There \fillintheblank{was a very, large, wet and disgusting looking dog} outside my door.

\end{enumerate}

\end{document}


Student Version:

Teacher Version:

-

A Variation on the answer at Create fill-in-the-blank version of a document with ability to toggle blanks on and off

It provides \answer for continuous underline (with line breaks) and \answerwords for word-by-word underline. Uncomment \TeacherCopy in the preamble, and get the teacher version.

EDIT, per Manuel's request, I leave extra space for the student's answer, which I accomplish by setting the fontsize to \Huge prior to commencing the underlining blackout, and then resetting it afterward (note that this does not affect the vertical height allocated for the answer, since the Huge text is converted into a vertically-challenged rule of the same width).

REEDITED so that teacher version provides same word spacing as student version.

The \censorruleheight and \censorruledepth can be altered to change the elevation the rule thickness and placement, respectively.

\documentclass{article}
\usepackage{censor}
\usepackage{xcolor}
\usepackage{ifthen}
\censorruledepth=-.2ex
\censorruleheight=.1ex
\newcounter{index}
\def\mysize{\Huge}
\def\TeacherCopy{%
\color{red}%
\getargsC{##1}%
\setcounter{index}{0}%
\whiledo{\theindex<\narg}{%
\stepcounter{index}%
\setbox0=\hbox{\mysize\csname arg\romannumeral\theindex\endcsname}%
\underline{$\vphantom{g}$%
\makebox[\wd0]{\csname arg\romannumeral\theindex\endcsname}}%
\mysize\ \normalsize%
}%
\unskip%
\color{black}%
}%
}
%\TeacherCopy
\parindent0pt
\begin{document}
\begin{enumerate}
\item And the answer to life, the universe, and everything, is \answer{forty two}.

\item The beginning six words of the Gettysburg Address are
\answerwords{Four score and seven years ago}.

\end{enumerate}
\end{document}


Student copy:

Teacher copy:

-
An extra space for the \answer command and an extra space for any word in the \answerwords would be great. Since handwriting is more spaced. – Manuel Mar 13 '14 at 10:56
@Manuel Done, per your request. – Steven B. Segletes Mar 13 '14 at 11:01
It's not OP's request. Just me making a suggestion :P – Manuel Mar 13 '14 at 11:10
censor` is nice, but one downside is that the positioning of the text when censored in this case is not the same as when uncensored. The blanks move, if you will. Not always an issue, but sometimes makes it more difficult to match things up quickly when you are looking at a whole page of these. – cslstr Mar 13 '14 at 11:25
@cslstr See revised answer. – Steven B. Segletes Mar 13 '14 at 12:04