I'm trying to build a script to semi-automatically fix orphan words. To do that I need a way to write an auxiliary file that contains some data. The idea is to have a command like:
Wor\OrphansSpotter[keyword]{d W}ord.\n\n
The function of such a command should be to write a line:
keyword X Y
in an auxiliary file, where X is the line number (of the page?) of the first char in the command argument and Y is the line number of the last char in the command argument.
In my temporary modified .tex file I shoud have some occurrences of such a command and my final auxiliary file content should be something like:
keyword1 15 15
keyword2 20 21
keyword3 3 3
keyword4 11 12
Scanning this file, if X!=Y, I could run a query-replacement command, e.g.
"Word Word." --> "Word~Word."
in my text editor (emacs).
Unfortunately I have no idea how to do it (I mean the LaTeX stuff).
\parfillskipand\emergencystretch? This not analyze in any way which word is, but can avoid last lines with less of X % of the test width, or even avoid incomplete lines of any length, like here. – Fran Mar 16 '20 at 18:18