Confused about \hrulefill vs. \rule and paragraph breaking

paragraphsrules

I've got a macro to insert a blank for fill-in-the-blank questions on my tests, but when it is at the beginning of a line, it breaks the paragraph right after the blank and the rest of the question moves down. Everything is working when the blank ends up in the midst or end of a line, though. I can get it to not break to a new paragraph, but I have to put a slashed space before it and I don't like the extra space.

My macro is:

\newcommand{\plongblank}%
     {\hbox to 1.2in {\rule[.1in]{0in}{.15in}%
                 {\hrulefill}}}%

(The rule is a strut to give the students room to write if this falls in a 2nd or later line of the question.)

I found and adapted this answer by egreg (Thanks!) to my needs, but I can't figure out why his works and mine doesn't? I'm guessing it is the penalty and +/- stretch, but I'd really like to know for sure and why.

Here's a MWE for completeness:

\documentclass{article}

\newcommand{\plongblank}%
     {\hbox to 1.2in {\rule[.1in]{0in}{.15in}%
                 {\hrulefill}}}%
\newcommand{\blank}[2][100]{\hfil\penalty#1\hfilneg\rule[.1in]{0in}{.15in}%
                                                   \rule[-3pt]{#2}{0.5pt}}

% commenting this out makes the paragraph break evident
%\setlength{\parindent}{0pt}

\begin{document}

When passing parameters to a function by value, any changes that the function
makes to the parameters
\plongblank{}
%\blank{1.2in}
reflected in the variables passed to the function in the calling function.

\plongblank{}
%\ \plongblank{}
%\blank{1.2in}
describes the functionality in C++ where 2 or more functions share the same
name, and the determination of which function is called is based on the number
and/or type of parameters that are being passed.

\end{document}

Best Answer

TeX puts typesetting objects to a list at each moment and it is working in horizontal (internal), vertical (internal/outer), paragraph (i.e. outer horizontal), math (internal/display) mode at each moment. It is more complicated but for understanding basic principle suppose only horizontal, vertical and paragraph modes. TeX changes current mode using stack and creates a list of typesetting objects in each mode: in horizontal or paragraph mode it creates horizontal list, in vertical mode it creates vertical list. So we have a tree with lists.

At the start of \hbox, TeX opens horizontal (internal) mode and puts the objects from \hbox to horizontal list. At the end of \hbox, TeX closes the horizontal list, creates a box consisting this list, leave the horizontal mode and puts the created box as a single object to the currently created list in "parent" mode. Whenever TeX sees \vbox, it does almost the same but opens and close vertical mode.

TeX starts creating the document in vertical mode. If the vertical mode is current and \indent, \noindent, \leavevmode or individual letter occurs then it opens paragraph mode. If paragraph mode is current and \par or empty line occurs, TeX finalizes paragraph mode: breaks created horizontal list to lines of the paragraph and puts these lines to the vertical list of the "parent" vertical mode. Switching between vertical and paragraph modes is basic principle for creating paragraphs.

Suppose now example: TeX is in vertical mode and sees \hbox{abc} followed by efg followed by empty line. The \hbox{abc} is put to the vertical list as single box and then individual letter e opens the paragraph mode. Empty line finalizes the paragraph mode, creates one line of the paragraph with indentation and efg letters and puts this line to the vertical list of the "parent" vertical mode. We have \hbox{abc} in this vertical list followed by the line <indent>efg. Most important: they are in the vertical list, so they are one above second. Suppose modified example: \leavevmode\hbox{abc}efg. Now TeX first opens paragraph mode and puts the box and the efg to its horizontal list. Important note: they are in the horizontal lists, i.e. they will be in a single line of the paragraph side by side.

Do experiments with this example and try to understand this:

\levevmode % comment it or not
\hbox{abc}
efg
\par

The switching between modes in basic feature of TeX. Unfortunately, is is ignored in most of manuals for beginners.

Related Question