[Tex/LaTex] How to horizontally align headers and decimal centered cells

dcolumnhorizontal alignmenttables

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
 & \multicolumn{1}{c}{Header 1} & \multicolumn{1}{c}{Header 2} \\
\hline
\hline
Length & 1.489 & 2.569 \\
Width & 5 & 2.4\\
\lasthline
\end{tabular}

\end{document}

The obtained table.

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?

Best Answer

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
 & {Header 1} & {Header 2} \\
\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

enter image description here

Related Question