# How do I center table headings and stretch the table to full column width?

I have the following code for creating a table in the multicols enviroment. I'd like to center the column headings and make it so that the table automatically takes up the entire column from edge to edge. Is this possible within tabular?

\documentclass[11pt, a4paper]{article}
\usepackage{multicol}
\usepackage{booktabs}
\usepackage[left=17.5mm,right=17.5mm,top=24.5mm,bottom=33.95mm]{geometry}
\setlength{\columnsep}{20pt}
\begin{document}
\begin{multicols*}{2}
\begin{center}
\begin{tabular}{ l  l  l }
\toprule
Property&Value&Unit\\
\midrule
Item description& 59& mm \\
Item description& 50& mm \\
Item description long& 76&mm\\
Item description&80&mm\\
Item description long& 20& mm \\
Item description long& 70& mm \\
Item description& 40&mm\\
Item description& 873& K \\
Item description long& 300& K \\
Item description&103,405 &Pa\\
\bottomrule
\end{tabular}
\end{center}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

\end{multicols*}
\end{document}

-
To center an individual entry (like a heading) independently from the column format you can use \multicolumn{1}{c}{Heading}. To save some space, you can say \let\mc\muticolumn in the preamble and use \mc1c{Heading}. –  Christian Lindig Mar 3 '12 at 19:45

Here is one attempt at doing this (I also centred the last two columns, since it seemed "appropriate"):

\documentclass[11pt, a4paper]{article}
\usepackage{multicol}% http://ctan.org/pkg/multicol
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
% The following package is loaded by tabularx
%\usepackage{array}% http://ctan.org/pkg/array
\usepackage{booktabs}% http://ctan.org/pkg/booktabx
\usepackage[left=17.5mm,right=17.5mm,top=24.5mm,bottom=33.95mm]{geometry}% http://ctan.org/pkg/geometry
\setlength{\columnsep}{20pt}
\begin{document}
\begin{multicols*}{2}
\begin{center}
\begin{tabularx}{\columnwidth}{ X  *{2}{>{\centering\arraybackslash}p{.25\columnwidth}} }
\toprule
\centering\arraybackslash Property & Value & Unit\\
\midrule
Item description& 59& mm \\
Item description& 50& mm \\
Item description long& 76&mm\\
Item description&80&mm\\
Item description long& 20& mm \\
Item description long& 70& mm \\
Item description& 40&mm\\
Item description& 873& K \\
Item description long& 300& K \\
Item description&103,405 &Pa\\
\bottomrule
\end{tabularx}
\end{center}
Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
\end{multicols*}
\end{document}


tabularx provides the stretchable X column type, while array allows you to insert <stuff> before cell entries on a column-by-column basis (using the >{<stuff>} prefix). You also have to specify the width of the tabularx as \columnwidth. The paragraph column types are set to a quarter of the column width.

The column specification

{ X *{2}{>{\centering\arraybackslash}p{.25\columnwidth}} }


identifies 3 columns. The first is an X column, which stretches to fill any of the remaining horizontal space (\columnwidth in the example). The following two are specified as identical using the *{<num>}{<col spec>} syntax. In general, this repeats <col spec> for <num> columns. The two columns are both paragraph style of width .25\columnwidth. Also, every cell is prepended with \centering\arraybackslash with the aid of the >{<stuff>} notation (provided by the array package, loaded by tabularx). Since every cell spans its own group, the \centering applies to the particular cell. The addition of \arraybackslash is to restore the correct use of \\ in tabular environments.

Here is an example of a 0.5:0.3:0.2 column spread with a left:centre:right alignment:

\begin{tabular}{
p{\dimexpr.5\columnwidth-2\tabcolsep}
>{\centering\arraybackslash}p{\dimexpr.3\columnwidth-2\tabcolsep}
>{\raggedleft\arraybackslash}p{\dimexpr.2\columnwidth-2\tabcolsep}
}
%...
\end{tabular}


Each column specification is given as p{<len>} which allows for exact measurement of the width <len> of the column. Additionally, 2\tabcolsep is removed from each column to accommodate for the gap between columns; added by default. Left-alignment (justified) is default in a p-column (alternatively you could use >{\raggedright\arraybackslash}p{<len>} to obtain non-justified, left-aligned output), while \centering and \raggedleft provides centred and right-aligned columns.

You would have obtained the same output if your replaced the first column specification with X and use

\begin{tabularx}{\columnwidth}{
X
>{\centering\arraybackslash}p{\dimexpr.3\columnwidth-2\tabcolsep}
>{\raggedleft\arraybackslash}p{\dimexpr.2\columnwidth-2\tabcolsep}
}
%...
\end{tabularx}


since X is a "stretchable p-column" (thereby removing some of the calculation in the code). In all of the above, \dimexpr<expression> allows one to perform calculations on dimensions (or lengths).

-
Thanks for your reply, I'm having trouble following the syntax of the { X *{2}{>{\centering\arraybackslash}p{.25\columnwidth}} } part, could you show me an example that specified the left column to be left aligned, the middle to be centered and the right as right aligned with corresponding widths of 0.5:0.3:0.2 just so I can see how it works a little better please? –  Andy Mar 3 '12 at 20:03
@Andy: I've added some detail in this regard. If it's not enough, let me know. –  Werner Mar 4 '12 at 6:22
That is perfect, thank you for such a detailed reply! –  Andy Mar 4 '12 at 10:16