# How to horizontally align headers and decimal centered cells

I use this code to produce a table and to align the column with the dot of the numerical values.

\documentclass{article}

\usepackage{dcolumn}

\begin{document}

\begin{tabular}{l D{.}{.}{3} D{.}{.}{3}}
\firsthline
\hline
\hline
Length & 1.489 & 2.569 \\
Width & 5 & 2.4\\
\lasthline
\end{tabular}

\end{document}


Two requests for this table:

I would like to have the headers properly centered with the numerical values.

How can I specify the width of the columns produced with the D function?

This is a job for siunitx. This package makes available S columns with many more features than those provided by dcolumn. For instance, your case can be treated with

S[table-format=1.3]


where 1.3 specifies one digit for the integral part and three for the decimal part. You can use exponential (also known as scientific) notation with proper alignment and change easily many aspects of data typesetting.

\documentclass{article}

\usepackage{siunitx,booktabs}

\begin{document}

\begin{tabular}{l *{2}{S[table-format=1.3]} }
\toprule
\midrule
Length & 1.489 & 2.569 \\
Width & 5 & 2.4\\
\bottomrule
\end{tabular}

\end{document}


I've added also the rules as drawn by booktabs

• That seems to do the job for the centering part, but what about the definition of the width? Is it possible to define the desired width of a S column? Jun 22 '13 at 21:55
• @LudovicC. I don't get it. Jun 22 '13 at 21:56
• Let say I would like to have the column header 1 with a width of 10 cm (even though that might not be aesthetic), can I do that? Jun 22 '13 at 21:57
• @LudovicC. \multicolumn{1}{c}{\makebox[10cm]{Header 1}} Jun 22 '13 at 22:02
• Nice. When I compile with pdflatex, siunitx forces several "passes", while Gonzalo's dcolumn solution doesn't. So unless I absolutely need siunitx I tend to go for dcolumn. Is this something others have noticed? Perhaps I'm experiencing interference with some other package. Jun 19 '18 at 13:00

One place before the decimal separator, three after it, so use D{1.3}

\documentclass{article}
\usepackage{dcolumn}
\usepackage{booktabs}

\begin{document}

\begin{tabular}{l D{.}{.}{1.3} D{.}{.}{1.3}}
\toprule