The tabularx package

Sets a table to a width you choose, for example \textwidth: the column type X takes the space that is left and wraps its text.

Loading the package

\usepackage{tabularx}

In tabular, the columns l, c and r are as wide as their longest cell and never break a line, so a cell with a sentence pushes the table past the margin. A p{4cm} column wraps, but you have to work out its width yourself. tabularx takes the total width as its first argument and computes the width of every X column from what the other columns leave.

Several X columns share the space equally. The text in an X column is justified like a p column; >{\raggedright\arraybackslash}X sets it left-aligned, which reads better in narrow columns. The package loads array.

Main commands

\begin{tabularx}{\textwidth}{lXr} ... \end{tabularx}Table with the width of the text area; the middle column fills the rest.
XColumn that wraps its text and whose width is computed.
>{\raggedright\arraybackslash}XX column with left-aligned text; \centering centres it.
\newcolumntype{L}{>{\raggedright\arraybackslash}X}Defines the letter L for that column, to keep the column argument short.
>{\hsize=1.5\hsize}X >{\hsize=0.5\hsize}XTwo X columns in the ratio 3 to 1. The factors have to add up to the number of X columns.
\renewcommand{\tabularxcolumn}[1]{m{#1}}Centres the other cells of a row vertically next to a wrapped X cell.

Complete example

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{l L r}
\toprule
Task & Description & Hours \\
\midrule
Survey  & Read the literature on the topic and write a summary
          of the methods that other groups have used. & 40 \\
Design  & Plan the experiment. & 12 \\
Writing & Write the report and prepare the figures. & 25 \\
\bottomrule
\end{tabularx}
\end{document}
Result
The compiled example

Open in the editor

Compiled with pdfLaTeX from TeX Live 2023.

Pitfalls

Related pages