10

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?

11

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

| improve this answer | |
  • 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? – Ludovic C. Jun 22 '13 at 21:55
  • @LudovicC. I don't get it. – egreg 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? – Ludovic C. Jun 22 '13 at 21:57
  • @LudovicC. \multicolumn{1}{c}{\makebox[10cm]{Header 1}} – egreg 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. – PatrickT Jun 19 '18 at 13:00
7

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

\end{document}

enter image description here

| improve this answer | |
  • I had to look a few times for the differences. – egreg Jun 22 '13 at 21:48
  • Ok for the centering. Do you know how to specify the with of a D column? – Ludovic C. Jun 22 '13 at 21:56

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.