[Tex/LaTex] Automatically stretch table to evenly fill horizontal space

tablestabularx

I want to have my tables fill the entire width of the page (i.e., \textwidth). So far, the only thing I've managed to come up with is:

\renewcommand{\tabcolsep}{4.4pt}

By manually trying different values, I can eventually get the table to spread out and fill the space. However, this is tedious as I have a lot of tables.

I tried the tabularx package:

\begin{tabularx}{\columnwidth}{ r r r r r r r }
\toprule
& $z_{6}$ & $z_{8}$ & $z_{9}$ & $z_{11}$ & $z_{13}$ & $z_{14}$ \\
\midrule
fileA & 0.00 & 0.00 & 0.00 & 0.08 & 0.79 & 0.08  \\
fileB & 0.01 & 0.00 & 0.13 & 0.00 & 0.84 & 0.00  \\
fileC & 0.00 & 0.39 & 0.02 & 0.49 & 0.00 & 0.00  \\
fileD & 0.75 & 0.08 & 0.00 & 0.00 & 0.00 & 0.00  \\
\bottomrule                             
\end{tabularx}

However, this still relies on the \tabcolsep value. (If the value is small, all the columns will still be close; if it's large, the columns will be more spread out.) What I'm looking for is an automatic solution for two things:

  1. the table to take the entire width of the page; and
  2. each column to be evenly spaced along the horizontal.

tabularx only does (1). How can I get (2)?

Best Answer

Other answers have shown how to use tabularx however your description was mistaken, tabularx never changes \tabcolsep. However if your sample data is typical I think you do want the inter-column space to stretch and allow the column widths to be based on the natural column widths. For this you want the standard LaTeX tabular* not tabularx.

\documentclass{article}
\usepackage{showframe}% http://ctan.org/pkg/showframe
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
% tabularx already includes the array package
%\usepackage{array}% http://ctan.org/pkg/array

\begin{document}
\noindent\begin{tabular*}{\columnwidth}{@{\extracolsep{\stretch{1}}}*{7}{r}@{}}
  \toprule
  & $z_{6}$ & $z_{8}$ & $z_{9}$ & $z_{11}$ & $z_{13}$ & $z_{14}$ \\
  \midrule
  fileA & 0.00 & 0.00 & 0.00 & 0.08 & 0.79 & 0.08  \\
  fileB & 0.01 & 0.00 & 0.13 & 0.00 & 0.84 & 0.00  \\
  fileC & 0.00 & 0.39 & 0.02 & 0.49 & 0.00 & 0.00  \\
  fileD & 0.75 & 0.08 & 0.00 & 0.00 & 0.00 & 0.00  \\
  \bottomrule                             
\end{tabular*}
\end{document}​
Related Question