# Have the first word of a sentence appear at the beginning of the next line instead of at the very end of the line

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)

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.

Thank you!

• Where did you "learn this is to be avoided" when you see "this all over scientific papers"? – Werner Aug 19 '16 at 20:07
• by far the most reliable way would be to edit the source, with a regular expression such as \s+([a-zA-Z][a-z]?)\s+ to  \1~ every one or two letter word will be followed by tilde if it was followed by space – David Carlisle Aug 19 '16 at 20:28
• @ulrich.liebig: Are you using LuaLaTeX? I think it should be possible to deal with this using a custom pre_linebreak_filter filter. – wilx Aug 19 '16 at 20:31
• You write: ‘There is obviously still space at the end of the paragraph, so the "In" could easily be placed in the next line.’ But the problem is not the space at the end of the paragraph, it's that pushing “In” to the next line will make too wide interword spaces, which is much worse, from a typographic point of view. – egreg Aug 19 '16 at 20:51
• @ulrich.liebig: Where is MS Word is there an option to avoid this behaviour? And who "figured this out in LaTeX"? Where are you getting your information from? – Werner Aug 19 '16 at 21:41

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).

% !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
"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}