10

I am trying to align some numbers in a tabular on the decimal point. I have been using the dcolumn option but given that my column title is a year, it aligns also the year on the decimal point, which I do not want.

Here is my MWE :

\documentclass{beamer}

\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\begin{frame}{title} 

\begin{table}[H]
\centering
\begin{tabular}{|l|d{4.6}|c|c|} % I just did it for one to see the result
\hline 
\textbf{Poste}  & \textbf{2000} & \textbf{2005} & \textbf{2010} \\  \hline
Alimentation -           b1  & -0.034***  & -0.036**  & -0.016***  \\
Alcool et Tabac -        b2  & -0.009***  & -0.006    & -0.011***  \\
Habillement -            b3  &  0.004     &  0.001    &  0.001     \\
Logement -               b4  & -0.067***  & -0.106*** & -0.073***  \\
Meuble/Entretien -       b5  &  0.019***  &  0.016*** &  0.001***  \\
Santé -                  b6  & -0.001     &  0.005    &  0.036     \\
Transport -              b7  &  0.066***  &  0.075*** & -0.015***  \\
Communication -          b8  & -0.001     & -0.006*** &  0.028***  \\
Loisir/Culture -         b9  &  0.017***  &  0.030*** &  0.012***  \\
Autres biens/services -  b12 & -0.025     & -0.016    & -0.359     \\
\hline
\end{tabular}
\end{table}

\end{frame}

\end{document}

And here is a snapshot showing how the year (2000) is aligned. How can I keep the other numbers aligned but the year centered (like 2005 and 2010) ?

enter image description here

  • @ChristianHupfer - The OP is already using the D column type (provided by the dcolumn package. The remaining issue is, what to do with the headers? – Mico May 15 '15 at 9:52
10

I'd suggest using siunitx (and booktabs for better horizontal rules).

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{siunitx,booktabs}

\begin{document}

\begin{frame}{title} 

\begin{center}
\addtolength{\tabcolsep}{-2pt} % or it would be overfull
\begin{tabular}{
  l *{3}{S[table-format=-1.3,table-space-text-post=***]}
}
\toprule
\textbf{Poste}  & {\textbf{2000}} & {\textbf{2005}} & {\textbf{2010}} \\
\midrule
Alimentation -           b1  & -0.034***  & -0.036**  & -0.016***  \\
Alcool et Tabac -        b2  & -0.009***  & -0.006    & -0.011***  \\
Habillement -            b3  &  0.004     &  0.001    &  0.001     \\
Logement -               b4  & -0.067***  & -0.106*** & -0.073***  \\
Meuble/Entretien -       b5  &  0.019***  &  0.016*** &  0.001***  \\
Santé -                  b6  & -0.001     &  0.005    &  0.036     \\
Transport -              b7  &  0.066***  &  0.075*** & -0.015***  \\
Communication -          b8  & -0.001     & -0.006*** &  0.028***  \\
Loisir/Culture -         b9  &  0.017***  &  0.030*** &  0.012***  \\
Autres biens/services -  b12 & -0.025     & -0.016    & -0.359     \\
\bottomrule
\end{tabular}
\end{center}

\end{frame}

\end{document}

As you see, in order to center a cell's contents in a numeric column, it's sufficient to type it between braces.

There's no reason for using a table environment.

enter image description here

You can certainly keep the vertical rules:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{siunitx}

\begin{document}

\begin{frame}{title} 

\begin{center}
\addtolength{\tabcolsep}{-2pt}
\begin{tabular}{
  |l| *{3}{S[table-format=-1.3,table-space-text-post=***]|}
}
\hline
\textbf{Poste}  & {\textbf{2000}} & {\textbf{2005}} & {\textbf{2010}} \\
\hline
Alimentation -           b1  & -0.034***  & -0.036**  & -0.016***  \\
Alcool et Tabac -        b2  & -0.009***  & -0.006    & -0.011***  \\
Habillement -            b3  &  0.004     &  0.001    &  0.001     \\
Logement -               b4  & -0.067***  & -0.106*** & -0.073***  \\
Meuble/Entretien -       b5  &  0.019***  &  0.016*** &  0.001***  \\
Santé -                  b6  & -0.001     &  0.005    &  0.036     \\
Transport -              b7  &  0.066***  &  0.075*** & -0.015***  \\
Communication -          b8  & -0.001     & -0.006*** &  0.028***  \\
Loisir/Culture -         b9  &  0.017***  &  0.030*** &  0.012***  \\
Autres biens/services -  b12 & -0.025     & -0.016    & -0.359     \\
\hline
\end{tabular}
\end{center}

\end{frame}

\end{document}

enter image description here

Now, compare the two renderings and take your pick. I believe you'll choose the “no vertical rule version”. ;-)

  • thanks, but how could I keep the vertical lines between columns ? – etienne May 15 '15 at 9:56
  • @etienne I added the “ruled” version. – egreg May 15 '15 at 10:28
  • 1
    I agree, the first one is the best. – etienne May 15 '15 at 10:28
2

One way is to wrote headers in \multicolumn{1}{c}{<header text>}:

\documentclass{beamer}

\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\begin{frame}{title}

\begin{table}[H]
\centering
\begin{tabular}{|l|d{4.6}|c|c|} % I just did it for one to see the result
\hline
\multicolumn{1}{c}{\textbf{Poste}}  
    &   \multicolumn{1}{c}{\textbf{2000}} 
        &   \multicolumn{1}{c}{\textbf{2005}}
            &   \multicolumn{1}{c}{\textbf{2010}}           \\  \hline
Alimentation -           b1  & -0.034***  & -0.036**  & -0.016***  \\
Alcool et Tabac -        b2  & -0.009***  & -0.006    & -0.011***  \\
Habillement -            b3  &  0.004     &  0.001    &  0.001     \\
Logement -               b4  & -0.067***  & -0.106*** & -0.073***  \\
Meuble/Entretien -       b5  &  0.019***  &  0.016*** &  0.001***  \\
Santé -                  b6  & -0.001     &  0.005    &  0.036     \\
Transport -              b7  &  0.066***  &  0.075*** & -0.015***  \\
Communication -          b8  & -0.001     & -0.006*** &  0.028***  \\
Loisir/Culture -         b9  &  0.017***  &  0.030*** &  0.012***  \\
Autres biens/services -  b12 & -0.025     & -0.016    & -0.359     \\
\hline
\end{tabular}
\end{table}

\end{frame}

\end{document}

For formatting of columns try to use siunitx package.

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.