Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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
1  
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
add comment

2 Answers

up vote 5 down vote accepted

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
 \setlength\extrarowheight{3pt} % extra padding
  %\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}
share|improve this answer
    
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
1  
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
add comment

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.

share|improve this answer
    
Thanks for that! I'll check it out! –  Amit Jul 21 '12 at 20:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.