TransWikia.com

Is there a way to automatically break boxes (e.g. raisebox) at the end of the line?

TeX - LaTeX Asked on August 19, 2021

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.3diebox}{textbf{#1}}}
    else
    raisebox{-0.5ex}{parbox{2.3diebox}{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.

One Answer

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}
newcommandblankit[1]{blankitaux#1 relax}
defblankitaux#1 #2relax{%
  blank{#1}%
  ifxrelax#2relaxdefnext{}elsedefnext{blankitaux#2relax}fi
  next
}
newcommand{blank}[1]{allowbreak
    setbox0=hbox{#1}%
    ifprintanswers
    makebox[2.3wd0][l]{textbf{#1}dotfill}%
    else
    raisebox{-0.5ex}{makebox[2.3wd0]{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

Answered by Steven B. Segletes on August 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP