1. Introduction
In this tutorial, we’ll explain how to wrap text in LaTeX tables. That is something we do when one or more cells contain several sentences that need to be broken up in a visually pleasing fashion.
2. An Example
Let’s have a look at a table too wide because of the cell content:
\documentclass[12pt]{article}
\begin{document}
\begin{tabular}{|l|l|}\hline
Persian Cat & Dachsund Dog\\\hline
A long-haired domestic cat, with a broad round head. &
A short-legged, long-bodied, hound-type dog breed.\\\hline
\end{tabular}
\end{document}
We see that the tabular environment sets the width of each column to the length of the longest text in it. That leads to a very wide table whose columns appear too far apart.
3. Controlling the Column Width
We can control the width of each column by using the p specification to tabular:
\begin{tabular}{|p{1.5in}|p{1.5in}|}\hline
Using this specification, we limit the width of each column to 1.5 inches:
While we obtain a well-proportioned table, the text is right-justified. As a result, we got unnatural spacing between words in the second column.
4. Using the ragged2e Package
We can suppress right-justification using the ragged2e package with the raggedrightboxes option:
\usepackage[raggedrightboxes]{ragged2e}
The option raggedrightboxes ensures that all boxes (or cells) in the table are ragged right:
We can see that result is more pleasing to the eye.
4.1. Finer Control Over Justification Using the ragged2e Package
Sometimes, we may wish to have different justification schemes for different cells of the table. Package ragged2e gives us this flexibility:
\documentclass[12pt]{article}
\usepackage{ragged2e}
\begin{document}
\begin{tabular}{|p{1.5in}|p{1.5in}|}\hline
\Centering{Persian Cat} & \RaggedRight{Dachsund Dog}\\\hline
\RaggedRight{A long-haired domestic cat, with a broad round head.} &
\RaggedLeft{A short-legged, long-bodied, hound-type dog breed.}\\\hline
\end{tabular}
\end{document}
This leads to the following table:
We can see the full flexibility available through the ragged2e package.
5. Manual Line Breaks
Sometimes we wish to put in line breaks manually. This is easily done with \newline:
\begin{tabular}{|p{1.5in}|p{1.5in}|}\hline
Persian Cat & Dachsund Dog\\\hline
A long-haired\newline domestic cat,\newline with a broad\newline round head. &
A short-legged, long-bodied, hound-type dog breed.\\\hline
\end{tabular}
This gives us:
This can be useful under many circumstances. Here’s an example:
\begin{tabular}{|p{1.6in}|p{1.6in}|}\hline
John Smith & Jane Smith\\\hline
\RaggedRight{A seventy-three-year-old grandfather:\newline {\em loves golf}}&
\RaggedRight{A sixty-five-year-old grandmother:\newline {\em suffers from diabetes.}}\\\hline
\end{tabular}
The newlines set off the italicized text:
This lets us emphasize lines as per our wishes.
6. The tabular* Environment
In some circumstances, we need to make a table with a specified width, regardless of the text in its cells. We may need to do this if the specification of a book or journal dictates a specific width. The tabular* environment lets us achieve this as shown:
\begin{tabular*}{5in}{@{\extracolsep{\fill}}|p{1.2in}p{1.2in}p{1.2in}|}\hline
Persian Cat & Dachsund Dog & Holstein Cow\\\hline
\RaggedRight{A long-haired domestic cat, with a broad round head.}&
\RaggedRight{A short-legged, long-bodied, hound-type dog breed.}&
\RaggedRight{A breed of large dairy cattle originating in northern Holland.}\\\hline
\end{tabular*}
We specify the desired table width (5 in) immediately after the {tabular*} command. As before, we leave the column specifications unchanged at p{1.2in}. There’s a special field @{\extracolsep{\fill}} that makes the p{1.2in} columns spaced out so that we obtain the full width of 5 inches:
7. The tabularx Environment
The tabularx environment lets us obtain uniformly spaced-out columns that add up to the specified width. It specifies a new column type “X” for this purpose.
\usepackage{ragged2e}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{5in}{|X|X|X|}\hline
Persian Cat & Dachsund Dog & Holstein Cow\\\hline
\RaggedRight{A long-haired domestic cat, with a broad round head.}&
\RaggedRight{A short-legged, long-bodied, hound-type dog breed.}&
\RaggedRight{A breed of large dairy cattle originating in northern Holland.}\\\hline
\end{tabularx}
\end{document}
We see that the columns are nicely spaced out:
We chose to use ragged right in this example but could have omitted this, obtaining fully justified cells.
8. Conclusion
In this article, we talked about wrapping text in table cells. We can wrap text in a LaTeX table by using the p specification in the tabular environment. Then, we’ll be able to break lines as per the specified width of the columns.
To obtain control over the justification of text, we can use the ragged2e package that permits us to produce pleasant-looking and compact tables. The tabular* and tabularx environments give us greater flexibility.