115

How do I prevent a line from appearing by itself:

Orphan: at the bottom of the page, or
Widow: at the top of the page?

  • 1
    there's a new (better) answer. Perhaps you want to reconsider? – Ooker Dec 13 '17 at 13:37
  • @Ooker, of the other 6 options, which is the one you now consider the better, or best? Or did kit already change it? – Jason Hemann Aug 15 at 21:25
  • 1
    @JasonHemann I use \usepackage[all]{nowidow} as ℝaphink suggested – Ooker 2 days ago
61

As Brent points out, you cannot always do this. The best you can do is to tell TeX that it's infinitely bad for these to appear:

\widowpenalty10000
\clubpenalty10000

One thing to keep in mind is that when presented with multiple infinitely bad options, TeX just picks one of them so you can still get widows or orphans.

  • I looked up somewhere else and they had \widowpenalty=10000. It didn't work then. Thanks! :) – Kit Oct 16 '10 at 1:30
  • 11
    @Kit (it's a late comment, but anyway) You don't need the = in the assignment, but it doesn't hurt. \widowpenalty=10000 and \widowpenalty 10000 are the same. – topskip Oct 4 '11 at 8:38
110

You can now use the nowidow package to make this task easier:

\usepackage[all]{nowidow}
  • 2
    Where do I find nowindow.sty, I don't seem to have it in my standard (Mac) TeX install... Thanks – Emit Taste Oct 25 '13 at 9:03
  • 12
    @EmitTaste - Note the spelling of the package: it's nowidow, not nowindow. – Mico Nov 8 '14 at 16:32
  • Does it also take care of orphans? – Raffi Khatchadourian Oct 11 '17 at 13:52
  • 2
    @RaffiKhatchadourian yes, it does. See the docs (in particular the \noclub and \setnoclub commands). – ℝaphink Oct 12 '17 at 14:53
  • @ℝaphink im trying to use these, but they seem to make no difference, and im not sure i understand the docs right. do i use \setnoclub like this: \usepackage[defaultlines=4,all]{nowidow} \setnoclub ? – fogx Jul 10 at 9:59
26

The Memoir manual, in section 3.5 "Sloppybottom" discusses this in some detail, which I won't reproduce here.

Be prepared even to re-word in the most intractable cases.


Update:

I think the specific commands like \enlargethispage and \sloppybottom are exclusively for the memoir package, but here's a snippet extracted from the aforementioned that you may care to adjust (you can see the extensive comments in the original):

\clubpenalty=9996
\widowpenalty=9999
\brokenpenalty=4991
\predisplaypenalty=10000
\postdisplaypenalty=1549
\displaywidowpenalty=1602

Personally, I tend to avoid this TinXering with Plain TeX internals; although I don't know how to do it specifically for newlfm, I'd probably opt for adjusting the textheight on a case-by-case basis, as a final tidy-up before publishing.

  • I'm using newlfm. Is this applicable, too? – Kit Oct 16 '10 at 1:18
  • 8
    while \sloppybottom is indeed memoir-specific (\raggedbottom is the comparable "plain" command), \enlargethispage is defined in base latex, so should be usable with any document class. – barbara beeton Sep 27 '11 at 13:27
  • I'm using KOMA Script, do you recommend to use those values there? – Aradnix Oct 27 '17 at 22:14
  • 1
    @Aradnix I can't say I'd recommend it, but it's certainly with a try (:-) – Brent.Longborough Oct 27 '17 at 23:28
  • I did it but also I need to increase the div value and adjust the BCOR (binding correction) factor for avoid the widow lines. Thanks. – Aradnix Oct 30 '17 at 20:22
7

This FAQ answer gives some tips, including enlarging/reducing the (double-)page, setting the paragraph tighter, using \raggedbottom (for which, see also this FAQ answer which discusses putting some stretch in the \topskip).

4

Sometimes, even with all of the suggestions above, you end up with an odd looking page, something without an apparent solution if you are not at the liberty of rewording the text. For those instances, I have developed a simple command, Kern, which allows to increase or decrease character tracking by fractions of a point.

% Kern
% #1 kerning amount
% #2 text
\newcommand{\Kern}[2]{\addfontfeature{LetterSpace=#1}#2\addfontfeature{LetterSpace=0}\null}%

So if you have some text, you would decrease tracking thus:

\Kern{-1.0}{Sometimes, even with all of the suggestions above, 
you end up with an odd looking page, something without an apparent
solution if you are not at the liberty of rewording the text.}

And, conversely, increase tracking thus:

\Kern{+1.0}{Sometimes, even with all of the suggestions above, 
you end up with an odd looking page, something without an apparent
solution if you are not at the liberty of rewording the text.}

Following up Nico's comment, this solution works with XeLaTeX and LuaLaTex only, and you need to have fontspec loaded.

  • You may want to make an explicit mention of the fact that this approach can only be employed if (a) the document is compiled with either XeLaTeX or LuaLaTeX and (b) if the fontspec package is loaded. It would also be helpful if you gave a couple of examples of how to use \Kern{...}{...}. – Mico Apr 29 '18 at 19:46
3

In order to make a club or widow line disappear, you can use solutions such as rewording or reordering, changing fonts or margins, or commands like \enlargethispage{1cm} or \enlargethispage*{1cm} (the latter “also shrinks any vertical white space on the page as much as possible, so as to fit the maximum amount of text on the page”, according to usrguide). But let's present less invasive possibilities here.

Influence the line breaking algorithm

TeX's algorithm for breaking a paragraph into lines offers a few knobs that can be very useful during the final phase of document preparation, because they can make paragraphs shorter or longer without any rewording, retaining good visual aspect especially if the paragraphs in question are long (if a paragraph is long enough, it's usually very easy for TeX to make it one line shorter or longer than what it would normally be, because there are many spaces that can be stretched or shrunk: at the very least, the interword spaces).

First, the integer parameter \linepenalty is used to compute the “demerits” of each line of a paragraph.1 Increasing \linepenalty makes every additional line in a paragraph more “expensive”, and therefore leads to shorter paragraphs. The value that matters is the value when the paragraph ends. Plain TeX and LaTeX set \linepenalty=10.2

There is another useful integer parameter that gives control over the number of lines in a paragraph (without changing the line length, paragraph shape, font, etc.): this parameter is called \looseness.3

If you say \looseness=<number> for a paragraph, TeX will try to make it <number> lines longer than what would have been done otherwise, without exceeding the current \tolerance. <number> may be negative (e.g., \looseness=-1), in which case TeX will try to make the paragraph shorter. Here is an example:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\looseness=-1 % Make the next paragraph one line shorter than normal.
              % Note the space token after -1. Otherwise,
              % you can write \looseness=-1\relax.
\lipsum[1-5]

\lipsum[6][1-2]
\end{document}

Without the \looseness change, the above document has two pages with only three words on the second page:

enter image description here

But simply setting \looseness as done in the above example allows the document to fit on a single page without changing margins or doing other ugly things:

enter image description here

Note that this works because the first paragraph of the document whithout any \looseness change is easy to shrink (its last line contains only one word, and the paragraph is not ridiculously short). When the change asked via \looseness is not possible without exceeding the \tolerance, \looseness has no effect. In other words, you can't take a two lines paragraph and expect TeX to shrink it by two lines by setting \looseness=-2!

The value of \looseness that matters is the one it has at the end of the paragraph you want to modify. \looseness is reset to zero at the same time as \hangindent, \hangafter, and \parshape are reset to their normal values, which is “at the end of every paragraph, and (by local definitions) whenever it enters internal vertical mode.”4

Therefore, the \looseness=-1 in the above example affects only the first of the five paragraphs produced by \lipsum[1-5]. There is no need to do this assignment inside a group.

If you want to make a paragraph as short as possible, you can try settings such as \looseness=-1000 or \linepenalty=100, the latter being less expensive in terms of computing time.5

The TeXbook also mentions a trick to spread some looseness among several paragraphs.6 Suppose that you want to make one of three paragraphs looser, but you don’t want to choose which one it will be. The trick is to combine the three paragraphs into a single one as far as TeX is concerned, and make it just look as if they were three paragraphs. To do this, simply end the first two paragraphs with a command named, e.g., \fakepar, and defined as follows:

\newcommand*{\fakepar}{%
  \hfil\vadjust{\vskip\parskip}\break\indent
}

This can be used in the lipsum example by doing \let\endgraf\fakepar like this:

\documentclass{article}
\usepackage{lipsum}

\newcommand*{\fakepar}{%
  % cf. TeXbook exercise 14.23, p. 315
  \hfil\vadjust{\vskip\parskip}\break\indent
}

\begin{document}
{
  % Try to make one of the following five paragraphs one line shorter
  \looseness=-1
  \let\endgraf\fakepar
  \lipsum[1-5]            % \fakepar is used *between* the paragraphs
}% Now comes the \par that terminates paragraph 5 of the \lipsum[1-5]

\lipsum[6][1-2]
\end{document}

I'm not quite sure why \let\par\fakepar doesn't work in this case, because according to the lipsum package documentation, it seems to me that \lipsum[1-5] uses \par separators by default between the paragraphs. If anyone has an idea, please edit this.

Influence the page breaking algorithm

What we discussed in the previous section can change the line breaks in a paragraph, and thus the number of lines occupied by a given paragraph (you can increase or decrease it, according to your needs and the possibilities offered by the particular paragraph). In order to avoid club or widow lines, another possibility is to work on page breaks without changing the appearance of any paragraph (apart from the fact that page breaks can split paragraphs), in particular without modifying the number of lines of any paragraph.

This can work if your pages have enough vertical space that can stretch or shrink. \parskip is often a good friend for this, because both plain TeX and LaTeX set \parskip=0pt plus 1pt, which gives one point of stretchability between any two paragraphs. List environments (itemize, enumerate, description...) and sectioning commands (\section, \subsection, etc.) are also good contributors to vertical stretchability and shrinkability in pages7, and so are display math formulas (\[ ... \], the equation environment, LaTeX's displaymath and amsmath's equation* environments).8

But if despite all these opportunities for vertical stretching and shrinking, you still have page breaks that produce a club or a widow line, it is possible to play with penalties in order to discourage page breaks in specific conditions. In particular, you might want to increase the value of \clubpenalty (to avoid page breaks after the first line of a paragraph) or \widowpenalty (to avoid page breaks before the last line of a paragraph).9 But:

  • LaTeX adds a layer in this area as compared to plain TeX; see the discussion about \@clubpenalty in this message from egreg;

  • be aware that there will be a price to pay, otherwise TeX wouldn't have produced such unpleasant page breaks in the first place!

Conclusion

Things to keep in mind:

  • \linepenalty and \looseness influence how lines are broken inside a paragraph;

  • \clubpenalty and \widowpenalty help TeX decide after which line it is going to break a page, but don't affect the appearance of paragraphs—apart from the particular lines after which a paragraph may possibly be split, between a page and the one that follows.

  • LaTeX often resets \clubpenalty to the value saved in \@clubpenalty. The value of \@clubpenalty is initialized from \clubpenalty at the beginning of the LaTeX run (see ltfiles.dtx); \clubpenalty is also copied to \@clubpenalty as part of the work done by \begin{document}. Then, during further document processing, \clubpenalty may be temporarily set to values such as 10000 by code from the LaTeX kernel, then automatically reset to the value stored in \@clubpenalty (see the definition of \@afterheading, a macro that is called by sectioning commands). Therefore, don't expect a change to the TeX parameter \clubpenalty to last in a LaTeX document, unless you do it in the preamble. If you want to perform a long-lasting change for this parameter after the preamble, better affect both \clubpenalty and \@clubpenalty.


Footnotes

  1. See TeXbook chapter 14, p. 98.
  2. See ltplain.dtx for the LaTeX source code.
  3. See TeXbook chapter 14, p. 103.
  4. See TeXbook chapter 14, pp. 103–104.
  5. See TeXbook exercise 14.25, pp. 104 and 316.
  6. See TeXbook exercise 14.23, pp. 103 and 315–316.
  7. See files such as article.cls for the source code of specific list environments, and size10.clo, size11.clo, size12.clo for the sectioning commands depending on the font size chosen in the \documentclass optional argument (10pt, 11pt and 12pt respectively).
  8. Look for paramaters \abovedisplayskip, \abovedisplayshortskip, \belowdisplayskip and \belowdisplayshortskip in the aforementioned size10.clo, size11.clo and size12.clo files.
  9. For other related demerits and penalties (penalty for a line ended at a discretionary break, demerit when two consecutive lines end with discretionary breaks, demerit for two adjacent visually incompatible lines, demerit when the second-last line of a paragraph ends with a discretionary, etc.), you may refer to chapter 14 of the TeXbook.
2

I'd been using the 10000 penalty for years (since 2005). This morning I learned the default was 150, and 500 was pretty aggressive. When I dropped from 10000 to 500, the widow/orphan lines went up (from 2 to 3). When I left it at default, the typical widow/orphan lines went toward 4...and new sections were more frequently shoved to the next page.

Now it looks the way I wanted to.

  • 6
    This reads more like a comment than a standalone answer. Please consider augmenting a posting a bit, e.g., to provide specific, tangible examples of what you mean by "the widow/orphan lines went up (from 2 to 3)." To make such a statement usable, one needs to know things such how many lines per page you have, the average length of your paragraphs, and if the widows/orphans appear in short or long paragraphs. – Mico Nov 8 '14 at 16:35
  • Sorry about that. I am specifically referring to a 6"x9" novel format. I target 39 lines-per-page. When I used the 10000 penalty, I would find frequent paragraphs (out of a 230-page novel) that had 2 line widows or orphans. By stepping down to the default (150) "fixed" it to 4 lines. The specific paragraph lengths of the work in question was closer to 8-10 lines per paragraph. So, instead of having 1 or 2 lines on one page and the balance on the other, the split was more amicable. Does that help? – Merovech Nov 9 '14 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.