# How to prevent globally and strictly Overfull by relaxing Underfull or justification?

To suppress overflows, on one extreme, we have ragged layouts which suppress justification :

\usepackage{ragged2e} % http://tex.stackexchange.com/questions/168480/why-does-tex-prefer-an-overfull-box-to-hyphenating-here
\RaggedRight


On the other extreme, we have standard justified text and one can use \sloppy which does not solve every problem anyway.

Both leads to ugly things for me.

At this point, I have come to:

\usepackage{hyphenat}
\hyphenpenalty=750
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[babel=true,kerning=french,protrusion=true,expansion=auto,spacing,tracking]{microtype}
\pretolerance=1500
\tolerance=2000
\setlength{\emergencystretch}{2em}


Which is the best solution I could come to, for my usage (French has compound words and apostrophes which deal badly with hyphenation - for compound words, I use \BreakableHyphen{}). Good discussion here and here.

But ideally, I would like standard tolerance and in some exceptional cases, relax the justification and replace it with a left alignment. But I want that parameterized in the preamble because I want the users to parameterize the format of the eBooks they will get, and want them to fit right without control. So local solutions are prohibited here.

What I imagine would be something like:

% previous lines identical
\pretolerance=300
\tolerance=500
\setlength{\emergencystretch}{2em}
pseudo-code: if (overflow) then underflow the current line by moving the last word to the next line


-
This is a very interesting question, but it could be improved immeasurably with the addition of compilable code beginning with \documentclass and ending with \end{document} to demonstrate some of the current issues you routinely face. –  Steven B. Segletes May 16 at 15:04
I forgot to precise I use LyX. I have to find how to make an example with LaTeX only... –  lalebarde May 16 at 16:40
I can make a LaTeX-only MWE, but I still don't manage to reproduce my line break problems. –  lalebarde May 16 at 17:39

If you are prepared to save each paragraph and re-process (or use luatex's callbacks) Then I think what you want to do is set the paragraph with a large emergency stretch so that there will be no overfull lines unless there is an unbreakable box (or word) that is wider than \textwidth.
Then disassemble the box line by line (using lua or a \lastbox loop) and find each line box that is very stretched: eg if the line is in \box0 use \setbox2\hbox{\unhbox0} then if box 2 is a lot shorter than \hsize use \box2 (so putting all the white space at the right) otherwise use \box0 keeping the original justified line, then move on to check another line of the saved paragraph until you have adjusted the whole paragraph.