How to force a table into page width?

You can use the tabularx package. It allows you to set the width of the table and provides the X column type, which fills out the rest of the space. It can be used for several columns, which then share the rest of the width equally.

Example:

\usepackage{tabularx} % in the preamble
% ....
\begin{tabularx}{\textwidth}{X|l}
  \textbf{Symptom} & \textbf{Metric} \\
\hline
Class that has many accessor methods and accesses a lot of external data & ATFD is more than a few\\
Class that is large and complex & WMC is high\\
Class that has a lot of methods that only operate on a proper subset of the instance variable set & TCC is low\\
\end{tabularx}

In general it is also possible to set the width of a column using p{<width>} instead of l as column type. Then it will be formatted as a paragraph and can include line breaks. Replace <width> with the required width.


Just to mention an additional method: the tabular* environment. Suppose you have a table with 6 center-aligned columns. You can force it to take up the full width of the textblock by setting it up as follows:

\begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}} ccccc}
...
\end{tabular*}

Unlike the tabularx and tabulary environments, which work by expanding the width of the columns, the tabular* environment works by expanding the intercolumn whitespace. Incidentally, when employing tabular* environments with the @{\extracolsep{\fill}} expansion device, one should NOT employ vertical rules in the table.

Personally, I suspect it's the need to remember to insert the directive @{\extracolsep{\fill}} that has kept the popularity of this approach quite subdued...


Disclaimer: Although tabu might still work in cases it’s development is kind of stuck and it is better to not use it (for new documents). See also Is the tabu package obsolete?

One can use tabu (e.g). It will set the table to a given width without needing to calc the ration by hand.

\documentclass{article}

\usepackage{tabu}
\usepackage{booktabs}% for better rules in the table

\begin{document}
\begin{tabu} to \textwidth {XXXX}
   \toprule
   xx & 1 & 2 & 3 \\
   \bottomrule
\end{tabu}
\end{document}

tabu comes with the new column type X which sets its width automatically. It has an optional argument taking l, r, c to adjust the alignment inside the cell or a numer to set uneven widths of columns. For example two columns, the first on right, the second one left aligned and twice the width of the first one, will be X[r]X[2] (l and 1 will be set by default). The part between to and {<cols>} can be any width, and the full part can be omitted to, i.e. \begin{tabu}{<cols>}.

tabu is compatible with longtable with the new environment {longtabu}.


Adding showframeand some text (lipsum) to the above example shows that the table has exactly the width of the text. On may notice that a table without a float environment is set inline and gets indented as every normal text, too. Use \noindent to prevent that.

table

\documentclass{article}

\usepackage{tabu}
\usepackage{booktabs}% for better rules in the table
\usepackage{showframe,lipsum}

\begin{document}
\lipsum[4]

\noindent
\begin{tabu} to \textwidth {XXXX}
   \toprule
   xx & 1 & 2 & 3 \\
   \bottomrule
\end{tabu}
\end{document}

Tags:

Margins

Tables