# How to automatically force latex to not justify the text when it is not wise?

I got the following example:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[brazil]{babel}

\usepackage{array,ragged2e}

\begin{document}

\section{Riscos}

\begin{tabular}{|p{2.5cm}|p{2.5cm}|p{2.0cm}|p{2.0cm}|p{2.5cm}|p{2.5cm}|}

\hline Riscos                       & Probabilidade & Impacto & Prioridade & Resposta            & Prevenção                    \\ \hline
\hline Problemas com notebook       & Baixa         & Médio   & Alta       & Usar desktop        & Manutenção preventiva        \\
\hline Problemas com o desktop      & Baixa         & Alto    & Alta       & Usar o labUFSC      & Manutenção preventiva        \\
\hline Problemas com perda de dados & Baixa         & Alto    & Alta       & Uso do backup       & Backup periódicos            \\

\end{tabular}

\end{document}


Which generates this PDF:

The letter o as pointed on the image, must not be placed/justified on the right of the space. I think latex should be able to identify such aberration.

My question is, are there some way to force latex automatically not justify the text when the words are too much sparse like on the example just above?

I searched a lot and found that I can manually force some words to not be badly justified, however I do not like the idea to do such manually because it could be done automatically by latex itself, when certain conditions are meet, i.e. certain spacing limits are crossed.

## Update

Answering the comment, I would to like latex to not justify the words if they go beyond a minimum width, and accordingly to the character count of the current line.

For example, if the line has less than 50 characters and the justification will create a hole bigger than something as 30 pixels or 6 characters, therefore the justification must not be applied for that line.

• I’d say that TeX avoids taking this kind of decision “automatically” by design. Could you specify in greater detail what behavior you’d like to implement? By “greater detail” I mean a precise description of the different courses of action that you want to be taken and of the conditions under which each of them should be applied. – GuM Apr 20 '17 at 23:19
• I updated the answer. What do you think about such criteria? Is it valid? Can latex do so? – user Apr 20 '17 at 23:31
• In other words, you’d want TeX to automatically retypeset a paragraph using \raggedright (or \RaggedRight) if and only if the justification gets “too much” bad, with “too much” specified in same (even rigorous) way. Unfortunately, I’m afraid that this clashes with a known limitation of TeX: that it provides no primitive to know, at run time, the total cost of line breaking in the most recent paragraph… See @gernot’s answer for a solution that always uses \raggedright/\RaggedRight. – GuM Apr 20 '17 at 23:46
• In TeX by Topic (eijkhout.net/texbytopic/texbytopic.html) there is an example of getting lines in a paragraph to be raggedright if they are too underful. See section 5.9.6. Perhaps this method would be suitable? – David Purton Apr 20 '17 at 23:50
• Oww, Thanks @DavidPurton, seems to be exactly what I want to, but sadly it seems not to be done automatically by latex. I could not understand how to use it, thought. – user Apr 21 '17 at 0:00

Here are three versions of the table using

• the packages array and ragged2e to insert \RaggedRight at the beginning of each cell; this leads to more possibilities for line breaks, which is useful for narrow columns.

• the package array to insert \raggedright at the beginning of each cell; this leads to less line breaks, but the text is flush left.

• the packages array and booktabs; this is like the second version, but without vertical lines.

array, ragged2e and \RaggedRight

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[brazil]{babel}

\usepackage{array,ragged2e}

\begin{document}

\noindent
\begin{tabular}{|*2{>{\RaggedRight\arraybackslash}p{2.5cm}|}
*2{>{\RaggedRight\arraybackslash}p{2.0cm}|}
*2{>{\RaggedRight\arraybackslash}p{2.5cm}|}}

\hline Riscos                       & Probabilidade & Impacto & Prioridade & Resposta            & Prevenção                    \\ \hline
\hline Problemas com notebook       & Baixa         & Médio   & Alta       & Usar desktop        & Manutenção preventiva        \\
\hline Problemas com o desktop      & Baixa         & Alto    & Alta       & Usar o labUFSC      & Manutenção preventiva        \\
\hline Problemas com perda de dados & Baixa         & Alto    & Alta       & Uso do backup       & Backup periódicos            \\

\end{tabular}

\end{document}


array and \raggedright

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[brazil]{babel}

\usepackage{array}

\begin{document}

\noindent
\begin{tabular}{|*2{>{\raggedright\arraybackslash}p{2.5cm}|}
*2{>{\raggedright\arraybackslash}p{2.0cm}|}
*2{>{\raggedright\arraybackslash}p{2.5cm}|}}

\hline Riscos                       & Probabilidade & Impacto & Prioridade & Resposta            & Prevenção                    \\ \hline
\hline Problemas com notebook       & Baixa         & Médio   & Alta       & Usar desktop        & Manutenção preventiva        \\
\hline Problemas com o desktop      & Baixa         & Alto    & Alta       & Usar o labUFSC      & Manutenção preventiva        \\
\hline Problemas com perda de dados & Baixa         & Alto    & Alta       & Uso do backup       & Backup periódicos            \\

\end{tabular}

\end{document}


array, booktabs, and \raggedright

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper, margin=2cm]{geometry}
\usepackage[brazil]{babel}

\usepackage{array,booktabs}

\begin{document}

\noindent
\begin{tabular}{*2{>{\raggedright\arraybackslash}p{2.5cm}}
*2{>{\raggedright\arraybackslash}p{2.0cm}}
*2{>{\raggedright\arraybackslash}p{2.5cm}}}
\toprule
Riscos                       & Probabilidade & Impacto & Prioridade & Resposta            & Prevenção                    \\
\midrule
Problemas com notebook       & Baixa         & Médio   & Alta       & Usar desktop        & Manutenção preventiva        \\
\midrule
Problemas com o desktop      & Baixa         & Alto    & Alta       & Usar o labUFSC      & Manutenção preventiva        \\
\midrule
Problemas com perda de dados & Baixa         & Alto    & Alta       & Uso do backup       & Backup periódicos            \\
\midrule

• It is nice to know, but I do not like hyphenations as they broken some acronyms and uncommon words. It prefer latex to left justify it, instead of. – user Apr 20 '17 at 23:45