# Short word after long word in narrow table cell

In a narrow raggedright table cell with a long word followed by a short word (for example "aaaaaaaaaaaaaa bbb"), if the first word is too wide and therefore hyphenated (using this solution), then the second word ends up in the third line even though it there's still enough space for it on the second line.

aaaaaaaaaa-
aaaa bbb

I see

aaaaaaaaaa-
aaaa
bbb

This can be avoided by using a ~ between the two words, but can it be avoided automatically?

Small example with column type taken from here:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\title{tmp}

\begin{document}

\maketitle

\begin{tabularx}{50pt}{|L{50pt}|}
\hspace{0pt} Fooooo-ooo bar
\end{tabularx}

\end{document}

(beware the word space after \hspace)

The reason for the extra line is that TeX avoids having a hyphen on the last linebreak in a paragraph, it's only a mild hint but here with so little else happening the demerits from having a hyphen there are enough to cause TeX to generate an extra line to avoid it. Normlly tex would have to over-stretch white space in order to get an extra line and the penalty from teh underfull lines would prevent this happening but here you have \raggedright which puts zero-penalty infinite stretch on every line, so the cost (as seen by TeX) of having a three line paragraph is rather low. If you set the hyphen demerits to 0 then it chooses a two line paragraph.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\title{tmp}

\begin{document}

\maketitle

\begin{tabularx}{50pt}{|L{50pt}|}
\finalhyphendemerits=0
\hspace{0pt}Fooooo-ooo bar

X\dotfill X
\end{tabularx}

\end{document}

With the help of the ragged2e package and its \RaggedRight command you can defne your L type column as shown in the following example.

In your original code, you used tabularx with a p type column of the same width. This causes an overfull box warning since the column will in fact be wider than the declared 50pt as 2*\tabcolsep (=2*6pt) will be added. \tabcolsep is the small hrizontal space between the vertical line and the start/end of the text. I have therefore added two alternative tables that do not produce this warning. The first alternative works entirely without tabularx, the second one uses a left aligned X type column.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}

\usepackage{ragged2e}

\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}}

\begin{document}

\begin{tabularx}{50pt}{|L{50pt}|}
Fooooo-ooo bar
\end{tabularx}

\bigskip

Alternative without tabularx:

\begin{tabular}{|L{50pt}|}
Fooooo-ooo bar
\end{tabular}

\bigskip

Alternative with tabularx's X type column:

\begin{tabularx}{50pt}{|>{\RaggedRight\arraybackslash}X|}
Fooooo-ooo bar
\end{tabularx}

\end{document}
• Apparently \let\newline\\ from here is indeed unnecessary when using ragged2e. – root Oct 26 '19 at 11:18
• Your solution works, but it hyphenates more rarely. i.e. it allows the long word to protrude extremely close to the vertical line without hyphenating the word, in cases where without ragged2e hyphenation occurs. Maybe the overfull hbox and the difference between the example and my big document are the reason. – root Oct 26 '19 at 11:34
• Shouldn't tex.stackexchange.com/a/12712 be edited accordingly? – root Oct 26 '19 at 11:36
• you changed the example text which completely changed the problem, the original problem is due to having a hyphen in the penultimate line of the paragraph. – David Carlisle Oct 26 '19 at 16:12
• @root yes because \RaggedRight only adds finite not infinite stretch so the cost of having three lines would be too high, but the code shown here doesn't show that. – David Carlisle Oct 26 '19 at 19:25