# smarter hyphenation for narrow columns?

I know how to turn off hyphenation via \nohyphens{.....} (from hyphenat). But what I'd really like would be to hyphenate smartly. What I mean by this should hopefully be made clear by the following example.

On the left is a piece of text from a column in a table, rendered with the \nohyphens{...} command. This leaves a lot of open white space. On the right is the same text typeset by vanilla XeLaTeX. As you can see, it still leaves a big white space in the middle of the text, and it also introduces the rather strange hyphenation, "or-phaning", instead of, for example, the more natural "cor-rections".

Here is a smarter version that is made by me introducing "cor-rections" into the source code:

To my eye at least, this seems much better than either of the versions above.

So the basic question is: Could I get LaTeX to do this for me automatically, for example by specifying the longest acceptable inter-word whitespace?

PS. Another more complex solution would be to specify a "target width" for the columns in my table, e.g. .25\textwidth and then have the system optimize to produce a table that brings me as close to my specified whitespace score and table width as possible, while minimizing the number of hyphenated words... but I don't know if LaTeX can do this sort of dynamic optimization in several variables! Here's one last image where I have gotten rid of the hyphenation entirely, by moving to .27\textwidth for columns. This seems to be the most readable version yet.

-
Kindly add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – texenthusiast Apr 5 '13 at 16:51
AFAIKT, the example is the picture -- this behavior should be familiar to anyone who uses LaTeX. But if it helps anyone, here's a Gist gist.github.com/holtzermann17/5320882 with a table that includes LOTS of inter-word white space. – Joe Corneli Apr 5 '13 at 17:00
narrow columns always pose a nasty problem for attractive justification. since the default hyphenation patterns produce cor-rec-tions, not to mention han-dled or-phan-ing mech-a-nism non-re-spon-sive au-thors, i'm wondering whether you're using them at all before trying \nohyphens. – barbara beeton Apr 5 '13 at 17:02
@barbarabeeton - I don't think I'm doing anything to change the default hyphenation. Here's the same table typeset using my actual preamble. This is a NON-minimal example, since I'm doing a lot of things in the preamble... but again, I don't think there's anything there to change the hyphenation patterns. gist.github.com/holtzermann17/5320983 (Should there be? I.e. I think this is what I'm asking in the question above - is there anything to do to get LaTeX to hyphenate more aggressively?) – Joe Corneli Apr 5 '13 at 17:15
For narrow columns raggedright (see ragged2e) will usually be better; if you insist on justified text, use microtype and LuaTeX/pdfTeX. – Martin Schröder Apr 6 '13 at 15:50

When trying to typeset text in very narrow measures, it's sometimes best (least worst?!) to use the \sloppy directive, as is done in the following example.

It can also be helpful in such situations to set \righthyphenmin2. (For English-language texts, the default value of this parameter is 3.)

\documentclass[12pt,letterpaper]{article}
\usepackage[showframe,margin=3.38in]{geometry}
% \textwidth = 8.5"-2*3.38" = 1.74"
\frenchspacing
\righthyphenmin2
\sloppy
\usepackage{fontspec}
\setmainfont[Ligatures=TeX]{Linux Libertine O}
\begin{document}
\noindent
Quality control is handled with corrections and the orphaning'' mechanism in case
of nonresponsive authors. Some articles are world-writeable, as in the wiki model.
\end{document}

-
I tried with the full example from the Gist, and it's not clear that \sloppy has that much of an impact. However, your point about \righthyphenmin is a good one. I think what really matters here is \lefthyphenmin, in particular, \lefthyphenmin=3 will solve the problem of the awkward "or-phaning". (Cf. tex.stackexchange.com/questions/40324/pdflatex-and-hyphenation) However, honestly this seems a bit more like a clever hack than a "smart" solution. As in your example and my postscript, you can get arbitrarily good typesetting by changing the region width. – Joe Corneli Apr 5 '13 at 18:18
@JoeCorneli - If you're allowed to switch from XeLaTeX to LuaLaTeX (you're advisor might object, who knows!), you could make use of the microtype package to even out the "grayness value" of the textblock. I'd call that a pretty smart solution. :-) Incidentally, I couldn't run the example you posted to github as it makes use of a few fonts I don't have on my system. – Mico Apr 5 '13 at 18:31
latest version of microtype seems to work with XeLaTeX too, indeed, it's the walk-through example in tlmgr help. Following the walkthrough didn't work for me, but sudo /usr/local/texlive/2012/bin/x86_64-linux/tlmgr update --all did. I've compiled the document with \usepackage[protrusion=true]{microtype} and some of the other tweaks and things are indeed looking a bit better... – Joe Corneli Apr 5 '13 at 19:45
@JoeCorneli - the more I contemplate the matter, the less convinced I am that setting \lefthyphenmin3 is a good solution. As you must know too, there are tons of English words that start with prefixes such as in, re, or un. For such words, it should be unobjectionable to hyphenate them after the prefix (in-quire, re-set, re-view, re-wind, un-clear, etc). Suppressing all hyphenation after two letters is bound to create more problems that it solves. If it's mostly about dealing with orphaning, you can always provide the instruction \hyphenation{orphan-ing} in the preamble, right? – Mico Apr 6 '13 at 5:40

I'm not sure why you are using \nohyphens I'd have thought you want more rather than less. Using the linked table as you don't supply code for the example in the question I get this which isn't too bad, although could obviously be tweaked a bit more.

\documentclass[article,a4paper,12pt,twoside]{memoir}

\usepackage{hyphenat}

\setlrmarginsandblock{40mm}{20mm}{*}
\setulmarginsandblock{20mm}{20mm}{*}

\begin{document}

\renewcommand{\arraystretch}{1.5}
\begin{table}
\begin{center}
\raisebox{3in}{\small
\begin{tabular}{|*{3}{>{\hyphenpenalty0 }p{.25\textwidth}|}}
\hline
% \multicolumn{1}{p{.25\textwidth}}{\textbf{Relevance}}
\begin{center}(PM)
\end{center}

Ultimately relevance depends on peer review, and irrelevant content
may be deleted. Mechanisms to ensure that relevant content
\emph{will} be added could be improved. &
\begin{center}(WP)
\end{center}
they're interested in; apart from this, rules like WP:WEIGHT come into
play.&
\begin{center}(DO)
\end{center}

Anyone can upload projects (for full projects'', one time approval
is needed), but getting changes into the core requires considerably
more vetting. \\
%\multicolumn{1}{p{.25\textwidth}}{\textbf{Quality}}
Quality control is handled with corrections and the orphaning'' mechanism
in case of nonresponsive authors. Some articles are world-writeable,
as in the wiki model. & Automated tools for spam and vandalism detection
combined with a system of editorial oversight, in which Jimmy Wales
has last say. & In addition to bug reports and feature requests handled
through the issue tracker, modules can make use of an automated patch
testing system.\\
%\multicolumn{1}{p{.25\textwidth}}{\textbf{Scalability}}
Peer review is distributed. Links are handled automatically. Caching
is deployed where relevant; in particular, interlinking features are
kept up to date. & The database and other infrastructure is massively
scaled. There are many bots that help with small tasks. & In theory,
anyone can join. Earl Miles, NYCCamp 2012 keynote: \emph{There are
no insiders, except Dries; there are no outsiders, only resumes.}''
\\
% \multicolumn{1}{p{.25\textwidth}}{\textbf{Consistency}}
Although automatic links and corrections can help with consistency,
mainly PM relies on standards for proof and expository quality.
&NPOV is the key rule, which works together with templates and other
process tools to maintain community standards about style and
content. & The project issue queues are the place to go when one
module's changes breaks another's. The core of the project has
considerable oversight in these
matters.\\
% \multicolumn{1}{p{.25\textwidth}}{\textbf{Motivation}}
People are solving some of their learning, exposition, and social
needs on the site by writing and reviewing articles and posting in the
forums.& As of 2006, over 50\% of the site had been written by less
than 1\% of the users; these days, paid editing is somewhat
notorious.& Miles continued: \emph{To build a resume, find someone
who needs help, and help them. Find something that needs doing, do
it.}'' \\ \hline
\end{tabular}
}
\end{center}
\caption{As typeset out of the box''}
\end{table}

\end{document}


If you allow a bit of flexibility on the right margin, and add microtype you get:

\usepackage{microtype}

...

\begin{tabular}{|*{3}{
>{\hyphenpenalty0 \rightskip0pt plus .3em \emergencystretch2em}%
p{.25\textwidth}|}}


-
my supervisor suggested to get rid of the hyphens because he didn't like the look of "or-phaning". But for this example it also serves another purpose -- it helps us see whether or not hyphenation actually improves things. It doesn't seem to help a lot, because there is still lots of whitespace. – Joe Corneli Apr 5 '13 at 18:02
@JoeCorneli - If your adviser can be placated if you write \mbox{orphan}\-ing, which suppresses the first of two possible hyphenation points in or-phan-ing, I'd go for it. More generally, to suppress all hyphenation instances after just two characters in a word, set \lefthyphenmin3. – Mico Apr 5 '13 at 18:16
@JoeCorneli since TeX only hyphenates to minimise the badness property of the paragraph, preventing hyphens will in almost all cases increase white space. – David Carlisle Apr 5 '13 at 22:19
@JoeCorneli Are you sure you should be taking advice on hyphenation and line breaking from a Word user? – David Carlisle Apr 5 '13 at 22:47
@JoeCorneli - There's a good reason why most Word users don't use hyphenation: It doesn't work all that well! It's certainly vastly inferior to TeX's hyphenation algorithm. Of course, no hyphenation algorithm will ever be perfect. In TeX, you always have the option to create hyphenation exceptions via the \hyphenation command (to be inserted in the preamble). Use - characters to indicate where hyphenation should be allowed in the words listed in the argument of the \hyphenation command. – Mico Apr 6 '13 at 5:50