Home > Explanations, Tips, Writing > Decimal alignment in LaTex tables

Decimal alignment in LaTex tables

So, I always look for this when I need it. However, I always keep forgetting it. So I decide to put it here for a little reminder.

Have you tried to align decimal in LaTex before? Did you do like this:

\begin{tabular}{r@{.}l}
Heading\\
3&14159\\
16&2\\
123&456\\
\end{tabular}


Have you try to type a lot of numbers using this notation? Painful, isn’t it? Is there an easier way?, you may ask. Yes there is, dcolumn to the rescue! The package dcolumn can save you a lot of typing time. The way to use it is really simple.

\usepackage{dcolumn}
\begin{tabular}{D{.}{.}{3.5}}
\multicolumn{1}{c}{Heading}\\
3&14159\\
16&2\\
123&456\\
\end{tabular}


Notice that in this example you need to use \multicolumn in the heading of that column. Nevertheless, dcolumn has a big notation, but you can replace it by a new definition using the \newcolumntype command. Then you would have something like this:

\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\begin{tabular}{d{3.5}}
\multicolumn{1}{c}{Heading}\\
3&14159\\
16&2\\
123&456\\
\end{tabular}


Hope this helps! :mrgreen:

About these ads
Categories: Explanations, Tips, Writing
1. No comments yet.
1. No trackbacks yet.