31

How to break a line of text (\newline or \\) and still maintain justification for its texts?

Hans
  • 573
  • Why do you want to do this? – Ian Thompson Oct 09 '15 at 10:39
  • only have one short three letter word in the new line if I don't force! Thus, I want to have 3 other words in the new line to make document look better. – Hans Oct 09 '15 at 10:41
  • 1
    Rather than force a linebreak it is better just to stop the break you do not want by ending the paragraph blah blah blah~abc. so it will not break before abc. – David Carlisle Oct 09 '15 at 10:58
  • A different way to fix the problem is to persuade TeX to set the text tighter, so paragraph is a line shorter than its natural length. Try adding \looseness=-1 at the start of the paragraph. This might not work on a short paragraph where TeX can't find a good way to break the lines. – alephzero Oct 09 '15 at 19:08

3 Answers3

41

\\ (which you should almost never use) is for leaving the line short. \linebreak (which you should almost never use) is the command to force a line and still justify the paragraph.

\linebreak has an optional argument, such that \linebreak[3] (or 2) will only break if the break makes the result not too spaced out.

Rather than force a linebreak it is better just to stop the break you do not want by ending the paragraph blah blah blah~abc. so it will not break before abc. That way if you edit the text and this is not needed at all it just acts as a space without affecting the paragraph breaking.

David Carlisle
  • 757,742
  • 3
    you should mention that \linebreak has an optional argument, such that \linebreak[3] (or 2) will only break if the break makes the result not too spaced out. – Frank Mittelbach Oct 09 '15 at 16:00
  • 1
    @FrankMittelbach done:-) – David Carlisle Oct 09 '15 at 16:11
  • You mention that one should almost never use \linebreak. I'm working in a multi-column environment right now, and the default behavior of latex is leaving many words overhanging, such that they intersect the subsequent column. Manually adding \linebreak fixes this, but is there some better way? – MRule May 18 '21 at 16:35
  • certainly there are better ways. do you have hyphenation set up correctly? that is the first thing to check, then try \raggedright or \sloppy or ... Adding manual line breaks should be the absolute last resort, it makes your tex document very fragile and any change to the page size you have to manually change every hand inserted line break. @MRule – David Carlisle May 18 '21 at 16:37
  • \raggedright is not quite what I was looking for, since it removes justification. \sloppy is better, but it removes all words that overhang. I'd like to stop words overhanging by so much that they collide with the next column. I'm a bit surprised that the default configuration of the article class doesn't handle this. The article I'm editing doesn't override any hyphenation settings, so if there is an issue here it is with the default configuration values. Maybe its a bug in my implementation? – MRule May 19 '21 at 07:23
  • If you are writing in German using the default US English hypenation rules TeX will not be able to sensibly break any long words so they will make overfull boxes, so you need to specify German (or whatever language) that's what I meant by setting up hyphenation. (I would say this is possibly the most common cause of bad linebreaking, so it was worth a guess). As for \sloppy that relaxes some things, if it is too much then obviously you can make a custom version that doesn't allow so much stretch but works for your line width and content. @MRule better to post a new question than ask here – David Carlisle May 19 '21 at 07:28
8

if such a short word is at the end of a paragraph, i usually recommend "tie"ing it to the previous word with ~. if that results in an overfull line, i then apply sloppypar to the paragraph and let tex find the "best" way to reflow the paragraph.

1

An example:

enter image description here

\documentclass{article}
\setlength\textwidth{7.9cm}    
\begin{document}

\noindent\textbf{Normal Text}\par
How to break a line of text (\verb|\newline| or \verb|\\|) and still maintain justification for its texts? here is one bad.

\bigskip

\setlength\parfillskip{0pt plus .4\textwidth}
\setlength\emergencystretch{.1\textwidth}

\noindent\textbf{Stretched Text}\par
How to break a line of text (\verb|\newline| or \verb|\\|) and still maintain justification for its texts? here is one bad.

\end{document}

The most beautiful solution is to re-word your sentence(s). Another solution would be to restrict the \parfillskip and allow some \emergencystretch to compensate for this in the rest of the paragraph.

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
  • 5
    I respectfully submit that the central spirit of LaTeX is that the user should focus on making the best possible content and letting the language take care of the formatting - so re-wording a sentence into a less natural one solely to get around a technical limitation in the language is very much contrary to the spirit of LaTeX. – tparker Nov 26 '17 at 03:43