5

I have tried for about an hour and a half now to get my table data to align by the decimal places and I am beginning to lose my mind a bit. I am aware of the siunitx and dcolumn packages but cannot get either to work for my table. Could someone please help? Thanks! Below is the code for my table:

\begin{table}
    \begin{center}
    \begin{tabular}{p{2.3cm}p{2.3cm}p{2.3cm}p{2.3cm}p{2.3cm}p{1.5cm}} \hline \hline
        Radius \newline ($R/R_\odot$) & Luminosity \newline ($L/L_\odot$) & Mass \newline ($M/M_\odot$) & Temperature \newline ($10^6$ K) & Density \newline (kg/m$^3$) & Pressure \newline ($P/P_c$) \\ \hline
        0.0 & 0.00 & 0.00 & 15.5   & 160000  & 1.00 \\
        0.1 & 0.42 & 0.07 & 13.0   & 90000    & 0.46 \\
        0.2 & 0.94 & 0.35 & 9.5     & 40000    & 0.15 \\
        0.3 & 0.99 & 0.64 & 6.7     & 13000    & 0.04 \\
        0.4 & 0.99 & 0.85 & 4.8     & 4000      & 0.007 \\
        0.5 & 0.99 & 0.94 & 3.4     & 1000      & 0.001 \\
        0.6 & 0.99 & 0.98 & 2.2     & 400        & 0.0003 \\
        0.7 & 0.99 & 0.99 & 1.2     & 80          & 4$\times10^{-5}$ \\
        0.8 & 0.99 & 1.00 & 0.7     & 20          & 5$\times10^{-6}$ \\
        0.9 & 0.99 & 1.00 & 0.3     & 2            & 3$\times10^{-7}$\\
        1.0 & 0.99 & 1.00 & 0.006 & 0.0003   & 4$\times10^{-13}$ \\ \hline
    \end{tabular}
    \end{center}
\end{table}

And here is what the current table looks like. Note that the decimal points are not aligned (see Temperature column): enter image description here

1 Answer 1

13

It's easy with siunitx:

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}
\centering
\addtolength{\tabcolsep}{-0.3pt}
\begin{tabular}{
  @{}
  S[table-format=1.1]
  S[table-format=1.2]
  S[table-format=1.2]
  S[table-format=2.3]
  S[table-format=6.4,group-digits=integer,group-four-digits]
  S[table-format=1.2e-2]
  @{}
}
\toprule
{Radius} & {Luminosity} & {Mass} & {Temperature} & {Density} & {Pressure} \\
{($R/R_\odot$)} & {($L/L_\odot$)} & {($M/M_\odot$)} &
  {(\SI[parse-numbers=false]{10^6}{K})} & {(\si{kg/m^3})} & {($P/P_c$)} \\
\midrule
0.0 & 0.00 & 0.00 & 15.5  & 160000  & 1.00   \\
0.1 & 0.42 & 0.07 & 13.0  &  90000  & 0.46   \\
0.2 & 0.94 & 0.35 & 9.5   &  40000  & 0.15   \\
0.3 & 0.99 & 0.64 & 6.7   &  13000  & 0.04   \\
0.4 & 0.99 & 0.85 & 4.8   &   4000  & 0.007  \\
0.5 & 0.99 & 0.94 & 3.4   &   1000  & 0.001  \\
0.6 & 0.99 & 0.98 & 2.2   &    400  & 0.0003 \\
0.7 & 0.99 & 0.99 & 1.2   &     80  & 4e-5   \\
0.8 & 0.99 & 1.00 & 0.7   &     20  & 5e-6   \\
0.9 & 0.99 & 1.00 & 0.3   &      2  & 3e-7   \\
1.0 & 0.99 & 1.00 & 0.006 & 0.0003  & 4e-13  \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

Tables with heterogeneous data are always difficult to typeset. If you don't want the space before the times sign in the last column, change the table preamble into

\begin{tabular}{
  @{}
  S[table-format=1.1]
  S[table-format=1.2]
  S[table-format=1.2]
  S[table-format=2.3]
  S[table-format=6.4,group-digits=integer,group-four-digits]
  S[table-format=1.0e-2,table-align-exponent=false]
  @{}
}

and the output will be

enter image description here

6
  • Awesome, thank you! I tried playing around with the package but couldn't get it to work myself. One question: How do I remove the spaces between the digit and the multiplication sign in the values at the bottom of the last column?
    – Spahn711
    Jul 17, 2015 at 21:17
  • 1
    @Spahn711 Good question! Change the last column specification into S[table-format=1.0e-2,table-align-exponent=false]
    – egreg
    Jul 17, 2015 at 21:24
  • Works perfectly! Thanks again, you're a life saver!
    – Spahn711
    Jul 17, 2015 at 21:56
  • @egreg S[table-format=1.0e-2] should be enough.
    – LaRiFaRi
    Jul 18, 2015 at 13:07
  • @egreg a brief question: what does @{} do in tabular environment? I've never seen this before. Is it siunitx related?
    – Leeser
    Jul 18, 2015 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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