2

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 :-)

  • 2
    It's not a solution. Either you justify all your paragraphs or use ragged right throughout. – egreg Nov 28 '14 at 17:00
  • 1
    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 02: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 03: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 03:43
  • Sorry for the slow reply, I was away from the computer for the weekend. @cfr: You're right, I only wish for a single line in the output to be non-justified (I wasn't aware that I was overloading the term with latex jargon). I see three options: either having the offending output line extend into the margin, or having it split and justified, but massively stretched, or having it split and ragged. I believe the last option to be least ugly. – sircolinton Dec 01 '14 at 00:11
  • @DavidCarlisle: Thanks for the suggestion. I had previously encountered \sloppy, but dislike the output--see my previous comment; using slopping would be the second option. – sircolinton Dec 01 '14 at 00:13
  • I think it is an extremely bad idea but the only thing you could do would be to insert explicit line breaks by hand. I do not think that you can have this handled automatically. As I understand it, TeX justifies or does not justify paragraphs. The format of the paragraph is set at the paragraph's end. (Line spacing etc. being an obvious example.) You can't apply different settings to different lines and have them be part of a single paragraph. – cfr Dec 01 '14 at 00:23
  • setting \emergencystretch alone may make better output than sloppy, or you could put a conditional fill after a box you know to be large that adds fill if there is a linebreak, I don't think it's easy to do that automatically spot where a line would be over-full. – David Carlisle Dec 01 '14 at 00:26
  • @cfr: I agree, I would be against manually setting line breaks. Thanks for explaining why it's presumably not possible with TeX. – sircolinton Dec 02 '14 at 12:28
  • @DavidCarlisle: Thanks. By and large, I don't like excessive stretching---I find it difficult to read overly stretched lines. However, it seems that stretching may be the best solution available in TeX. I'm curiously surprised that I'm unusual in preferring an occasional ragged line to an occasional overstretched line. Oh well :-) Many thanks for all your comments! (and to cfr too) – sircolinton Dec 02 '14 at 12:34

2 Answers2

2

enter image description here

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}
David Carlisle
  • 757,742
  • 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 03 '14 at 17:41
1

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.