6

I am looking for a ConTeXt solution that will allow me to move single letters from the end to the beginning of the line.

LuaLaTeX has

https://ctan.org/pkg/luavlna or https://ctan.org/pkg/impnattypo packages

Algorithm in Lua

one-letter word at the end of line

1 Answers1

7

You can almost copy-paste the code from the linked answer, you just have to know where to put it. (Also works in LMTX, at least in 2020.07.30 01:02)

\startluacode
function userdata.prevent_single_letter(head)
    local cur = head
    while cur do
        if cur.id == node.id("glyph") then
            if cur.prev.id == node.id("glue") and cur.next.id == node.id("glue") then
                local p = node.new("penalty")
                p.penalty = 10000
            -- This is for debugging only, but then you have to
            -- remove the last node.insert_after line:
            --local w = node.new("whatsit","pdf_literal")
            --w.data = "q 1 0 1 RG 1 0 1 rg 0 0 m 0 5 l 2 5 l 2 0 l b Q"
            --node.insert_after(head,cur,w)
            --node.insert_after(head,w,p)

            node.insert_after(head,cur,p)
        end
    end
    cur = cur.next
end
return head, true

end

\stopluacode

\mainlanguage[pl]

% Ugh ... don't do this for real
\noheaderandfooterlines
\setuplayout[width=8.2cm]
\setupwhitespace[big]

\starttext

Noc była sierpniowa, ciepła i słodka, Księżyc oświecał srebrnem światłem
wgłębienie, tak, że twarze małego rycerza i Basi były skąpane w blasku.
Poniżej, na podwórzu zamkowem, widać było uśpione kupy żołnierzy, a także i
ciała zabitych podczas dziennej strzelaniny, bo nie znaleziono dotąd czasu na
ich pogrzebanie.

\startluacode
nodes.tasks.appendaction("processors", "before", "userdata.prevent_single_letter")
\stopluacode

Noc była sierpniowa, ciepła i słodka, Księżyc oświecał srebrnem światłem
wgłębienie, tak, że twarze małego rycerza i Basi były skąpane w blasku.
Poniżej, na podwórzu zamkowem, widać było uśpione kupy żołnierzy, a także i
ciała zabitych podczas dziennej strzelaniny, bo nie znaleziono dotąd czasu na
ich pogrzebanie.

\stoptext

Red ellipses were added later enter image description here

Henri Menke
  • 109,596