# Avoid just nearly filled last lines

Old school typesetters never allowed the last line of a paragraph to be just nearly filled. Either it would be clearly shorter than the other lines or it would be stretched to align with the right margin. (I assume the standard value where they decided to stretch the line was the depth of the indentation.)

If you have the time the same result can be achieved by checking all pages and manually adding \parfillskip=0pt to all offending paragraphs.

For example, this document:

\documentclass{article}
\usepackage[paperwidth=3in,paperheight=3in]{geometry}
\begin{document}
\thispagestyle{empty}
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
sasdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdfxx
\end{document}


renders to a paragraph with an almost-full last line:

I've earlier been told that it would most probably be impossible to get pdfLaTeX to do this automatically.

If this is true -- could it be done with LuaTeX?

-
I............don't............think...........it's...........a..........good....‌​........idea. – percusse Jan 17 '12 at 20:51
@percusse Note that I only want to stretch these last lines by 1em (the typical indentation) which shouldn't be more -- I don't have the actual figures handy -- than also the other lines in the paragraph will be stretched. – Florian Jan 17 '12 at 21:00
Sorry if that was slightly rude. What I meant is that the same old school typesetters also (AFAIK) start handling the issue by considering the whole paragraph via eyeballing the pleasing cheats here and there. Probably that is the reason why there needs to be some sort of a human hand touching the issue. – percusse Jan 17 '12 at 21:04
Perhaps related: Ensure minimal length of last line – cmhughes Jan 17 '12 at 21:11
@percusse No offense taken! You are definitely right in the matter: Visual control is needed, but it would anyway be nice not to have too many things to correct manually. And if anybody could come up with a solution, that would take the rest of the paragraph into account, that would clearly be better than only stretching the last line! – Florian Jan 17 '12 at 21:21

I'm not sure I'm serious here, but if you are able (and or willing) to retypeset your paragaphs by collecting them first then there is one feature of TeX that would allow you to measure this and act upon it: TeX knows about the length of the last line of a paragraph (or partial paragraph to be precise) if that is followed by a math display. In that case the length of the line above the display is known within the formula as \predisplaysize. Thus you could

• collect the paragraph material in a macro, say
• trial typeset it and check out how close its last line is to the full line
• depending on the result retypeset the paragraph in earnest using a suitable value of \parfillskip, e.g., set it to 0pt or set it to 2em plus 1fil or whatever.

Here is the code to check it out:

\newdimen\mydim
\def\checkit{%
\mydim\hsize
\typeout{There is \the\mydim\space  space available on the last line}%
}

asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdfxx
$$\checkit$$


The 2em are substracted because the \predisplaysize is the length of the last line + 2em. For your example that gives us:

There is 4.98819pt space available on the last line


There are some special conditions: if the last line is not typeset at its natural width then the size given as \maxdimen, e.g., if \parfillskipwas set to 0pt, and if there was no previous line then it is set to -\maxdimen but both cases could be controlled in your situation.

-

Adding a \parfillskip that has a minimum length and infinite stretchability seems like it would do it.

For example, the below sets parfillskip such that the end of the paragraph must have AT LEAST whitespace equal to the minimum of 2 x characters:

\documentclass{article}
\usepackage[paperwidth=3in,paperheight=3in]{geometry}
\begin{document}
\thispagestyle{empty}
%% Measure the width of 'x' in this font
%% and set \parfillskip equal to two times
%% that width.
\setbox0=\hbox{x} \parfillskip=2\wd0 plus1fil

asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
sasdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdf sadf
asdfjk adsf af dsasdf f dasf fdsa fds afsd fdsaf asdfsdafdsa asdf dsaf asdfxx
\end{document}


Commenting out the \parfillskip=2\wd0 plus1fil line gives:

But it shows this when parfillskip is set:

-
ex is a vertical measure, I think you meant em? – morbusg Jan 17 '12 at 22:01
Good catch - I updated it so parfillskip is set based on the width of the x character. – Justin Bailey Jan 17 '12 at 22:24
This indeed solves the problem, but I'd prefer a solution that doesn't add extra lines... – Florian Jan 17 '12 at 22:41
Was this question ever answered? I am interested whether any automatic method listed above worked in the original aim of automatically flushing right any end of paragraph line followed by at least so much whitespace in the line. – user34902 Aug 11 '13 at 14:31