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. |
X | Column that wraps its text and whose width is computed. |
>{\raggedright\arraybackslash}X | X 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}X | Two 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}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A table of width
\textwidthat the start of a paragraph is moved right by the paragraph indentation and gives Overfull \hbox (263.664pt too wide) in paragraph at lines 3--6. Write\noindentbefore it or put it into atableenvironment. - When the other columns already fill the width, the log shows "X Columns too narrow (table too wide)". Shorten the other columns or make them
Xcolumns too. \begin{tabularx}inside the definition of a new environment stops with File ended while scanning use of \@xdblarg. Write\tabularx{...}{...}and\endtabularxin the definition instead.\verbin a cell gives the warning "\verb may be unreliable inside tabularx". Use\textttthere.- A
tabularxdoes not break across pages. For that, the packagexltabularcombines it withlongtable.
Related pages
- How-to Set the column width of a table in LaTeX
- How-to Fit a table that is too wide in LaTeX
- How-to Line break in a table cell in LaTeX
- Package
tabulary - Package
array - How-to Write a CV in LaTeX