[Tex/LaTex] Ensure minimal length of last line

frenchline-breakingparagraphsspacingtypography

This question led to a new feature in a package:
impnattypo

One rule in French typography is that the last line of a paragraph should not be shorter than the double of the indentation of the next paragraph.

Is it possible to specify that, or least considering that all paragraphs are indented of the same value, to make sure the last line is longer than twice this value?

A LuaTeX-specific solution is welcome, too.

Best Answer

You asked for a LuaTeX solution and you get one:

\documentclass{article}
\usepackage{luatexbase,luacode}

\begin{luacode}
local PENALTY=node.id("penalty")
last_line_twice_parindent = function (head)
  while head do
    local _w,_h,_d = node.dimensions(head)
    if head.id == PENALTY and head.subtype ~= 15 and (_w < 2 * tex.parindent) then

        -- we are at a glue and have less than 2*\parindent to go
        local p = node.new("penalty")
        p.penalty = 10000
        p.next = head
        head.prev.next = p
        p.prev = head.prev
        head.prev = p
    end

    head = head.next
  end
  return true
end

luatexbase.add_to_callback("pre_linebreak_filter",last_line_twice_parindent,"Raphink")
\end{luacode}


\begin{document}
\parindent = 2cm
\emergencystretch = \hsize

A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel.

The charm of existence in this spot, which was created for the bliss of souls like mine.

I am so happy, my dear friend, so absorbed in the exquisite sense of mere tranquil existence, that I neglect my talents. I should be
incapable of drawing a single stroke at the present moment; and yet I feel that I never was a greater artist than now.

\end{document}

What it does is it adds ties (~) between the words at the end of a paragraph, when the distance to the end is less then 2*\parindent. If the \parindent is large enough, the paragraphs will get ugly.

ugly paragraph


Note: this does not prevent hyphenation. So the minimal amount is not enforced by an hbox or so. This is an exercise left to the ambitious reader.

Edit: do not hardcode glyph ids...