The dcolumn package
Provides the column type D, which aligns the numbers of a table column at the decimal marker.
Loading the package
\usepackage{dcolumn}In an r column, 12.5 and 3.75 end at the same position, so their decimal points do not line up. A D column puts the decimal marker of every number at the same horizontal position.
D{.}{,}{2} has three arguments: the marker you type in the source, the marker that is printed, and the number of decimal places to reserve. The third argument can also be 3.2 for three digits before and two after the marker. Most documents define a short name once: \newcolumntype{d}[1]{D{.}{.}{#1}}.
The cells of a D column are set in math mode. The package loads array. The S column of siunitx solves the same problem and also rounds and formats the numbers.
Main commands
D{.}{.}{2} | Column aligned at the decimal point with room for 2 decimal places. |
D{.}{,}{2} | Reads a point in the source and prints a comma. |
D{.}{.}{3.2} | Reserves 3 digits before and 2 after the point. |
D{.}{.}{-1} | Negative number: the decimal point is centred in the column. |
\newcolumntype{d}[1]{D{.}{.}{#1}} | Short name, used as d{2} in the column argument. |
\multicolumn{1}{c}{Price} | Header cell of a D column as normal centred text. |
Complete example
\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\begin{document}
\begin{tabular}{l d{2} D{.}{,}{2}}
\hline
Item
& \multicolumn{1}{c}{Price}
& \multicolumn{1}{c}{Preis} \\ \hline
Bolt & 0.35 & 0.35 \\
Belt & 12.5 & 12.5 \\
Motor & 149.00 & 149.00 \\ \hline
\end{tabular}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A header written directly into a
Dcolumn is set in math mode: the word appears in italics with the letter spacing of a formula. Wrap it in\multicolumn{1}{c}{...}. - The package aligns the numbers as you type them. It does not round and does not add missing zeros; 12.5 stays 12.5 next to 149.00.
Related pages
- How-to Align numbers at the decimal point in LaTeX tables
- Package
siunitx - Package
array - How-to Align text in table cells in LaTeX