# Avoiding hbox overflow problems by falling back to ragged text

I would like the text in my LaTeX document to be justified, unless it would result in an hbox overflow (i.e., words overflowing into the margin). If an hbox overflow would otherwise occur, I would like LaTeX to format that line as ragged right. That is, I would like hbox underflows to be used in favour of hbox overflows.

Does anyone know if this is possible?

Very many thanks :-)

• It's not a solution. Either you justify all your paragraphs or use ragged right throughout. – egreg Nov 28 '14 at 17:00
• You can avoid overfull boxes by use of \emergencystretch (or \sloppy) so long as you do not have single boxes that are wider than the page. – David Carlisle Nov 28 '14 at 17:34
• @DavidCarlisle I thought that the OP meant 'line' in terms of output, but won't your suggestion work only for 'lines' of input (i.e. paragraphs)? [That is, I thought LaTeX only did this stuff at the level of paragraphs.] – cfr Nov 29 '14 at 2:37
• @cfr I don't understand your comment? The settings are paragraph settings, but that's what the OP is asking for, linebreaking (which is a paragraph feature). I'm not sure what you mean by lines of input, lines in the input source are not normally relevant (as end of line is treated like a space?) – David Carlisle Nov 29 '14 at 3:21
• @DavidCarlisle Perhaps you are right but I think the OP wants only that particular line in the output to be non-justified. I think the OP is not aware that that is impossible. [About the 'input' I meant what TeX reads as a line i.e. what becomes a paragraph when typeset. But I don't think that is 'line' in the sense the OP is using it. But I have been awake much, much, much too long.] – cfr Nov 29 '14 at 3:43

Possibly the closest to what you ask is to allow a small amount of raggedness to the right margin. The alternative of course, if manual correction is feasible, is to add an explicit \\ before the offending word on line 5.

\documentclass{article}

\begin{document}

\def\t{ short words and \texttt{veryVeryLongWords}}
\def\tst{\t\t\t\t\t\t\t\t\t\t\t\t}

{normal\par\tst\par}

{s-str\par\emergencystretch\textwidth\tst\par}

{sloppy\par\sloppy\tst\par}

{rr\par
\rightskip0pt plus .8pt
\tst\par}
\end{document}
• Thanks for the examples; I think that ultimately, manually introducing a line break with '\\' may be the only way to go to achieve my aim. Many thanks for your helpful comments :-) – sircolinton Dec 3 '14 at 17:41

From the comments by cfr, it seems that TeX can't do what I was requesting because it must format the whole paragraph identically, so either the whole paragraph must be ragged, or the whole paragraph must be justified. Thanks to DavidCarlisle for suggesting \emergencystretch and \sloppy.