LaTeX produces bad, widowed line

I just found a widowed line in my PDF document, created by LaTeX. It looks something like this, see "sion" on the second page:

``````| blah blah expres- |
|        8          |
---------------------
---------------------
|sion.              |
|                   |
|                   |
|2.2.1   Title A    |
|2.2.1.1 Title B    |
| Foo foo foo foo   |
|foo foo foo foo foo|
|foo foo foo foo foo|
|foo foo foo foo foo|
|foo foo foo foo foo|
|         9         |
---------------------
``````

Shouldn't LaTeX have avoided this? What could be the reason for this typography error?

-
Provide a minimal example. Typesetting with TeX is about a balance of different effects. The standard widow penalty in LaTeX discourages but does not forbid widows. – Joseph Wright Dec 23 '10 at 12:08
The question has already been asked here, but it's nice to also have the answers to this new question. – Hendrik Vogt Dec 24 '10 at 8:36

2 Answers

TeX assigns a badness to each line. Widows and orphans are bad and TeX tries to avoid them whenever possible. However, if avoiding the widow or orphan causes something else to be even more bad, TeX will insert the widow or orphan.

You can tell TeX that these are infinitely bad by using

``````\widowpenalty=10000
\clubpenalty=10000
``````

TeX still might insert these lines, but only if every other alternative is also infinitely bad (in which case, it just picks one of them).

It's worth pointing out that it's impossible for every piece of text to be typeset in such a way as to avoid bad breaks. Some lines/paragraphs simply have to be rewritten. That said, the algorithm that TeX uses is optimal with respect to the parameters set (for example the penalties Lev mentioned).

-
I guess it's a typo that your `\clubpenalty` is still 10x larger than the `\widowpenalty`. – Hendrik Vogt Dec 24 '10 at 8:16
@Hendrik. Makes no difference here: 10 000 or higher is 'infinite' in a penalty, so the two are treated identically by TeX. – Joseph Wright Dec 24 '10 at 8:48
@Hendrik: Oops! – TH. Dec 24 '10 at 9:29
@Joseph: That was clear to me, but using different (identically handled) infinities doesn't make things clearer ... – Hendrik Vogt Dec 24 '10 at 13:42

As TH wrote, your situation would pick up a `\widowpenalty` due to being a single line of a paragraph on a new page. It is also penalized as a page break after a hyphenation by `\brokenpenalty` and by `\finalhyphendemerits` for being a hyphenated second-to-last line of the paragraph. If any or all of these are set to sufficiently large values, then your bad break should be strongly discouraged and the only reason this break occurred is that TeX could see no better alternative, and you will have to give it some manual guidance.

-
Good point about the other penalties. – TH. Dec 24 '10 at 4:09