[Tex/LaTex] Have the first word of a sentence appear at the beginning of the next line instead of at the very end of the line

formattingline-breaking

I don't want a sentence to start at the end of a line with just one word, especially a very short word, like "It", "As", "The", etc.
It interrupts the flow of reading and just looks ugly.

Edit: After reading some comments, I added an example of what it looks like in MS Word, just to clarify:
Quote from Wikipedia
(Quote from Wikipedia)

Is there a way to have this automated for the whole document? I wrote a 100 page paper and I'd rather not do it manually for each case, if there was another way.

I learned that this is to be avoided, but I even find this all over scientific papers, like the random one I show below. There is obviously still space at the end of the paragraph, so the "In" could easily be placed in the next line. (Not to discredit this paper, which I haven't even read!)

Edit: If this concept is new to you, I don't want to make you insecure about your writing style! It might just be my personal aesthetic preference, really. Also note that I first learned to avoid this writing German, not English.
I highlighted the passage.

Thank you!

Best Answer

You mention that you don't use LuaLaTeX. The following answer may, hopefully, still be of use to other readers of this posting who are willing and able to use LuaLaTeX.

The solution sets up a Lua function that prohibits a linebreak immediately following 1-, 2-, or 3-letter words which (a) follow a punctuation mark and (b) begin with an uppercase letter. A few examples: A, I; Am, As, At, Be, By, Do, He, In, Of, Or, Us, We; And, Are, For, Her, His, She, Now, The. The Lua function is assigned to LuaTeX's process_input_buffer callback; it thus runs at a very early stage of processing, before TeX's usual processing kicks in.

Operation of the function is suspended if the text occurs inside a verbatim-like environment such as verbatim (provided by the LaTeX kernel), Verbatim (from the fancyvrb package), and lstlisting (from the listings package).

enter image description here

% !TEX TS-program = lualatex
\documentclass[12pt]{article}

%% Lua-side code
\usepackage{luacode}
\begin{luacode}
in_verbatim = false
function short_words ( s )
  if string.find ( s , "\\begin{[vV]erbatim}" ) or string.find ( s, "\\begin{lstlisting}" ) then
    in_verbatim = true
  elseif string.find ( s , "\\end{[vV]erbatim}" ) or string.find ( s , "\\end{lstlisting}" )then
    in_verbatim = false
  elseif in_verbatim == false then
    s = string.gsub ( s, "(%p)%s+(%u%l?%l?)%s+" , "%1 %2~" ) 
  end
  return s
end
\end{luacode}
%% TeX-side code
\AtBeginDocument{\luadirect{luatexbase.add_to_callback ( 
  "process_input_buffer", short_words , "short_words" )}}

% just for this example
\usepackage[textwidth=0.1mm]{geometry}
\setlength\parindent{0pt}

\begin{document}
off. The next  

truth? In that case 

off. This fact 

\begin{verbatim}
truth? In that case 
\end{verbatim}

truth? In that case 
\end{document}

Addendum: If the document does not contain any verbatim-like environments, the contents of the luacode environment may be simplified considerably:

\begin{luacode}
function short_words ( s )
    return ( string.gsub ( s, "(%p)%s+(%u%l?%l?)%s+" , "%1 %2~" ) )
end
\end{luacode}
Related Question