1

I am new to LaTeX so I am thankful for any help:

Whenever you type normal text in LaTeX it automatically breaks onto the next line if the end of the line is reached. However if you use a box (such as \raisebox) towards the end of the line it forces the box into the same line, even if the end of the line is reached. (As far as I know this results in an "Overfull \hbox" warning.)

\documentclass{article}
\begin{document}
This is some text that automatically goes in the next line if the end of the line is reached.
\raisebox{0.1ex}{This is some text that does not automatically go in the next line if the end of the line is reached. This even goes above the edge of the paper.}
\end{document}

Is there a way to automatically break the box at the end of the line and continue with the box in the next line? If not is the at least a way to automatically put the whole box onto the next line?

Background: I am a teacher and I am trying to create "fill in the blanks"-tasks for my students. I do this by typing normal text and then I use the following command:

\newlength{\diebox}
\newcommand{\blank}[1]{
    \settowidth{\diebox}{#1}
    \ifprintanswers
    \raisebox{0.1ex}{\parbox{2.3\diebox}{\textbf{#1}}}
    \else
    \raisebox{-0.5ex}{\parbox{2.3\diebox}{\hrulefill}}
    \fi}

(\ifprintanswers comes from the exam package)

I use the factor "2.3" so my students have more space to write there answers.

This does work as intended, except at the end of the line where my answers / the hrulefill doesn't break onto the next line.

Bernard
  • 271,350
Marvin
  • 11
  • 1
    A box cannot be broken...that is one of the virtues of a box. While the rest of the text (outside the box) can be made to readjust to margins with the use of the sloppypar environment or \sloppy macro, there remains a problem in your MWE that the box content itself spans more than a single \linewidth. In such a case, nothing can be automatically done...rather you would have to manually break the box into two. – Steven B. Segletes Sep 16 '20 at 15:54
  • 2
    If you can better explain your actual application or need, perhaps a means could be suggested that avoids the problems brought on by the use of the box. – Steven B. Segletes Sep 16 '20 at 15:56
  • 1
    @StevenB.Segletes Updated more information. =) – Marvin Sep 16 '20 at 16:09

1 Answers1

1

I get around the problem by using recursion to create a new box for each word in the answer, instead of creating just one large hbox. I also have to \allowbreak between boxes. Furthermore, the use of the 2.3 multiplier will tend to break margins, and so I employ \sloppy to avoid this.

\documentclass{exam}
\newcommand\blankit[1]{\blankitaux#1 \relax}
\def\blankitaux#1 #2\relax{%
  \blank{#1}%
  \ifx\relax#2\relax\def\next{}\else\def\next{\blankitaux#2\relax}\fi
  \next
}
\newcommand{\blank}[1]{\allowbreak
    \setbox0=\hbox{#1}%
    \ifprintanswers
    \makebox[2.3\wd0][l]{\textbf{#1}\dotfill}%
    \else
    \raisebox{-0.5ex}{\makebox[2.3\wd0]{\hrulefill}}%
    \fi
}
\begin{document}
\sloppy
This is some text that automatically goes in the next line if the end of the line is reached.
\blankit{This is some text that does not automatically go in the next line if the end of the line is reached. This even goes above and beyond the edge of the paper.}
Returning to normal text.

\printanswerstrue This is some text that automatically goes in the next line if the end of the line is reached. \blankit{This is some text that does not automatically go in the next line if the end of the line is reached. This even goes above and beyond the edge of the paper.} Returning to normal text. \end{document}

enter image description here

  • 1
    I have no idea what you did but it works. You are a genius! Thank you for your time and help! – Marvin Sep 16 '20 at 16:56
  • @Marvin \blankit passes the \relax-terminated argument to the workhorse macro, \blankitaux. The macro \blankitaux, after allowing a line break, strips off a word (in #1) and applies your \blank macro to it. Then if the remainder of the argument (#2) is not empty, it just sends it right back to \blankitaux one more time. – Steven B. Segletes Sep 16 '20 at 17:04