# Automatically stretch table to evenly fill horizontal space?

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)?

-
Use \begin{tabularx}{\textwidth}{XXXXXXX}. – Yiannis Lazarides Feb 16 '12 at 19:55

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}​

-
A perhaps slightly better result, without the need of inserting \extracolsep, can be obtained by loading the package widetable and saying \begin{widetable}{\columnwidth}{*{7}{r}} – egreg Feb 16 '12 at 20:28
@David Carlisle: Perfect, just what I was looking for! – stepthom Feb 16 '12 at 20:37

You're not using the tabularx environment "properly". tabularx provides an X column type that stretches as necessary. In order to obtain a right-aligned X column, you can use

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}


Here is a minimal example showing your full-width table:

\documentclass{article}
\usepackage{showframe}% http://ctan.org/pkg/showframe
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\usepackage{booktabs}% http://ctan.org/pkg/booktabs
% tabularx already includes the array package
%\usepackage{array}% http://ctan.org/pkg/array
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\begin{document}
\noindent\begin{tabularx}{\columnwidth}{ *{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{tabularx}
\end{document}​


The inclusion of showframe is merely to show that the table spans the entire column width.

Also note the reduced column specification when you're using similarly-specified columns: *{<num>}{<col spec>} duplicates <col spec> a total of <num> times. The newly-defined column type R inserts \raggedleft before each cell entry, pushing the contents flush right.

-
Thanks for your help and the pointer about different column types in tabularx. However, this still didn't work for me in all cases. For example, if my first column has really long strings (i.e., filenames), they aren't displayed correctly and just are displayed on top of the second column's characters. Also, if some of the cells have really long strings, then the strings are wrapped even when they don't have to be. – stepthom Feb 16 '12 at 20:39