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
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: