Monday, 13 September 2010

LaTeX: Hyphenate single long words in narrow tabular columns

Consider the following LaTeX code:

\documentclass{article}
\usepackage[english]{babel}
\begin{document}
\begin{center}
    \begin{tabular}{ | p{2cm} | l | l | p{5cm} |}
    \hline
    Incredibilidable & foo & bar & baz \\
    \hline
    \end{tabular}
\end{center}
\end{document}

When you render it, the first column overflows on the second one because 'Incredibilidable' is considered to be the first word of a paragraph, and LaTeX does not hyphenate first words of paragraphs. So here's what you get: 
And this doesn't look nice.

To fix this, we're going to tell LaTeX our word is no longer the first of the paragraph. To do that, we insert a zero length horizontal space before it:

  \hspace{0pt}Incredibilidable & foo & bar & baz \\

The rendering now turns into:

Et voila!



No comments:

Post a Comment