Automatically stretch table to evenly fill horizontal space?
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}
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.