[Tex/LaTex] Penalty or badness for linebreak near start or end of sentence

badnessboxesfootnotesline-breaking

I would like to set a penalty or badness for breaking a sentence (or the contents of some \command{...}) too early. More specifically, I would like the penalty of a line break to be dependent on the position in the sentence that the line break occurs.

For example, I might want an extra penalty of 500 for breaking a sentence within the first 10em, and an extra penalty of 100 for breaking a sentence within the last 5em. I would then be able to tune it so that LaTeX would only break a sentence near the beginning if it really needed to.

Is this possible to achieve? Even if it requires some unbox hackery I don't really mind.

Edit:

If I could do the following pseudocode, this was what I was thinking (at the beginning of each footnote):

\setlength{\remaining}{remaining space left on line}
\setlength{\needed}{length of hbox with desired text}
\let\extrapenalty=0
\while \needed > \remaining
    \if \remaining < 10em
        \addto{\extrapenalty}{500}
    \endif
    \addtolength{\needed}{-\remaining}
    \setlength{\remaining}{\textwidth}
    \if \needed < 5em
        \addto{\extrapenalty}{100}
    \endif
\endwhile
\penalty{-\extrapenalty} % encourage break here

Background to question: I am using paragraph formatted footnotes in memoir, but I find the line breaks to be undesirable. I don't mind a long line being broken in the middle, but I find it looks particularly bad when broken at the start.

For example:

1. This is a relatively long
sentence  so if it breaks in
the middle it's okay. 2. The
problem is when it's  broken
right at the start.  It just
looks wrong. 3. This, on the
other hand, is probably okay.

Best Answer

That's actually pretty hard to do in TeX. The penalty for hyphenating words or for breaking on a word space are per-paragraph settings so to change them mid-sentence you may have to make some compromises.

The easiest thing to do would be to stick the start of the sentence in a box

\mbox{2. The problem} is when it's broken

will not break after the the however this is an absolute prevention of breaking so probably will cause over-full boxes or bad line breaks and even if the line break happens at a good place it prevents the word space stretching or shrinking so will give uneven spacing within the line.

The linebreak penalty at a word space can be controlled if you insert explicit penalty so you can go

2.\nolinebreak[2] The\nolinebreak[2] problem\nolinebreak[2] is when it's broken

which will discourage linebreaks at the first two spaces (you could use some macro to parse ahead to find the first two or three spaces in each footnote and add these automatically) but this doesn't affect hyphenation so probably makes it more likely that teh word hyphenates (since breaking after the word is less desirable). Obviously The won't hyphenate anyway but in general that may be a concern. It may be best to prevent hyphenation by boxing each word separately.

2.\nolinebreak[2] \mbox{The}\nolinebreak[2] \mbox{problem}\nolinebreak[2] is when it's broken

Again, a macro could be inserted into your footnote code that did this automatically for the first two or three words.


Something like

\def\myfootnote#1{\footnote{\threewords#1}}

\def\threewords#1 #2 #3 {%
\mbox{#1}\nolinebreak[2]
\mbox{#2}\nolinebreak[2]
\mbox{#3} }

But perhaps with some extra checks in case the footnote does not have three spaces in it.

Related Question