5

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            \\
    \hline Problemas de Saúde           & Baixa         & Alto    & Alta       & Tratamento adequado & Cuidados diários apropriados \\ \hline

    \end{tabular}

\end{document}

Which generates this PDF:

enter image description here

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.

user
  • 4,745
  • 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
  • 1
    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
  • 1
    In TeX by Topic (http://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 00:00
  • @user, I think it should be possible to make a new table column type that does this automatically. If I can figure it out, I'll post an answer. But someone more knowledgeable than me can probably do it more quickly. – David Purton Apr 21 '17 at 00:04
  • Could you do it for normal text? On the article the says This macro can be called as "\vbox{ ... some text ... \par\eatlines}" or it can be inserted automatically with \everypar. How would I use that \everypar? May be I should open a new question? – user Apr 21 '17 at 00:10
  • @user, it's not so easy for normal text because I think \lastbox can't be used in normal text. It needs to be in the \vbox. But by all means ask others. – David Purton Apr 21 '17 at 00:18
  • @DavidPurton, I created a new question asking about the \everypar on https://tex.stackexchange.com/questions/365818/how-could-the-everypar-justification-statement-be-used – user Apr 21 '17 at 02:21
  • regardless of the justification, i think the baselines in the table are too far apart -- it looks like each line is a separate entry, not a continuous text within a cell. – barbara beeton Apr 21 '17 at 14:48
  • @barbarabeeton Now you said so, I also noticed it. I think it is because the interline spacing is set to \linespread{1.5}. Then I would just how to figured it out how to change its value inside the table. – user Apr 23 '17 at 22:49

1 Answers1

5

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

enter image description here

\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            \\
    \hline Problemas de Saúde           & Baixa         & Alto    & Alta       & Tratamento adequado & Cuidados diários apropriados \\ \hline

\end{tabular}

\end{document}

array and \raggedright

enter image description here

\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            \\
    \hline Problemas de Saúde           & Baixa         & Alto    & Alta       & Tratamento adequado & Cuidados diários apropriados \\ \hline

\end{tabular}

\end{document}

array, booktabs, and \raggedright

enter image description here

\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
    Problemas de Saúde           & Baixa         & Alto    & Alta       & Tratamento adequado & Cuidados diários apropriados \\
\bottomrule
\end{tabular}

\end{document}
gernot
  • 49,614
  • 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
  • @user See the second version that I just uploaded. – gernot Apr 20 '17 at 23:46
  • @user I'd choose the third version without vertical lines. Looks just nicer. – gernot Apr 20 '17 at 23:50
  • I prefer the version 2. I like the vertical lines, as without them seems something is missing. – user Apr 21 '17 at 00:02