# Having a table use textwidth and still have centered text?

I'm wondering if it's possible to have a table in LaTeX that spans the entire textwidth, and that the columns automatically center themselves... I have tried using the "X" option in tablerx, but it doesn't center the columns...

Additionally, you'll see that I place two \hlines below my first row. Is it possible to put a little space below the line?

Code below.

\begin{table}[h]
\caption{Unit comparison between reduced Lennard-Jones units and GROMACS dimensional units.  \vspace{0.2cm}}
\begin{tabularx}{\textwidth}{X X X X}

\hline \hline %\vspace{0.1cm}
\bf{Unit}          &              \bf{Lennard-Jones}              &             \bf{Dimensional}          &       \bf{GROMACS units} \\ \hline
Time             &                    1 $t^*$                       &                   1 ps                  &                     ps              \\
Velocity        &                    1 $v^*$                      &                1000 m/s            &                   ps/nm          \\
Temperature &                  1 $T^*$                      &                120.272 K          &                        K
\\ \hline \hline
\end{tabularx}
\label{table:properties}
\end{table}

-
change the X to >{\centering\arraybackslash}X to center the text in each column and add \setlength\extrarowheight{3pt} before the table to pad the rows a bit –  David Carlisle Jul 20 '12 at 19:06
oh don't use \begin{table}[h] unless you really have to, it makes it highly likely the table goes to the end of the document, use [htp] –  David Carlisle Jul 20 '12 at 19:09
Remember that \bf is an obsolete command: use \textbf. By the way, \bf is a declaration, so if you use it that way, you'll find all text in boldface. It's not a problem in a table cell, which forms a group, but it would be in normal text. –  egreg Jul 20 '12 at 19:37

I added a commented version of your code below. Please in future always post complete documents including the preamble and necessary packages etc. It makes it a lot easier to test the code.

\begin{table}[htp]% not h om its own
\caption{Unit comparison between reduced Lennard-Jones units and GROMACS dimensional units.  \vspace{0.2cm}}

\newcolumntype{C}{>{\centering\arraybackslash}X}% centering
%\bf does not take an argument, and has been deprecated
% since latex2ecame out in 1993 use \textbf

\noindent % otherwise the line will be too wide by \parindent
\begin{tabularx}{\textwidth}{C C C C}

\hline \hline
\textbf{Unit}          &              \textbf{Lennard-Jones}              &             \textbf{Dimensional}          &       \textbf{GROMACS units} \\ \hline
Time             &                    1 $t^*$                       &                   1 ps                  &                     ps              \\
Velocity        &                    1 $v^*$                      &                1000 m/s            &                   ps/nm          \\
Temperature &                  1 $T^*$                      &                120.272 K          &                        K
\\ \hline \hline
\end{tabularx}
\label{table:properties}
\end{table}

-
My apologies for not posting the complete source.. I still can if needed. Let me try out your solution. –  Amit Jul 20 '12 at 19:20
Everything worked! Thank you so much. Didn't know about the htp either! –  Amit Jul 20 '12 at 19:22
Are you sure one needs the \noindent instruction? (Isn't \parindent equal 0 inside a table float?) –  Mico Jul 20 '12 at 20:47
@Mico oh yes actually that's true thanks. Doesn't do any harm though:-) –  David Carlisle Jul 20 '12 at 21:26

You can also use the tabu package instead of tabularx and define your table as follows:

\tabulinesep=1.5pt
\begin{tabu} to \textwidth {X[c] X[c] X[c] X[c]}
\hline \hline
\rowfont[c]\bfseries %will apply the \bfseries command to all elements of the next row
Unit & Lennard-Jones & Dimensional & GROMACS units \\
\hline
Time & 1 $t^*$ & 1 ps & ps \\
Velocity & 1 $v^*$ & 1000 m/s & ps/nm \\
Temperature & 1 $T^*$  & 120.272 K & K \\
\hline \hline
\end{tabu}


You will need to add to your preamble \usepackage{tabu}

The \tabulinesep bit increases the space between lines (both above and below).

The way the syntax works for the X Column in tabu is X[<width modifier>,<column modifiers>] where the column modifiers can be l, c, r, j, L, C, R, J (left, centered, right, justified) with the default set to j. There are many more options in the manual.

-
Thanks for that! I'll check it out! –  Amit Jul 21 '12 at 20:11