# Adding colour to table cells when decimals are aligned with arrays and {.} used in the column definition

I've got a table (below) that aligns the decimal points of my data - I wish to highlight a value (2 adjacent cells), however this leaves a white space where the decimal point is! If I increase the left or right overhang it eventually cover the decimal point completely!

Here's the basic table:

\begin{tabular}{m{30mm}r@{.}lr@{.}lr@{.}lr@{.}l}

Point 1 & -0&0569& 0&0138& -0&1133& 0&1275      \tabularnewline

Point 2 & -0&1239& -0&0189& -0&1092& 0&1662 \tabularnewline

Point 3 &  0&0209& 0&1032& -0&2188& 0&2428  \tabularnewline

Point 4 & -0&3064& -3&2415& 0&1353& 3&2588  \tabularnewline

\end{tabular}


-
Hi Ollie, welcome to tex.sx. I took the liberty to correct the formatting of your post. You need to indent code segments by four spaces or use the '0101' button above the text field to do it for you. See tex.stackexchange.com/editing-help for more details. –  Martin Scharrer Feb 15 '11 at 13:51

You can use the siunitx package, which defines a new column type S that aligns numbers on the decimal point and can be configured quite extensively. The strength of siunitx is really in typesetting units, so it might be a bit oversized if you just need it for tabular material. (Judging from your example, you probably have numbers and units in your document, though, so you might want to take a look at the package).

\documentclass{article}
\usepackage{siunitx}
\usepackage{colortbl}

\begin{document}
\sisetup{
table-figures-integer = 2,
table-figures-decimal = 4,
table-number-alignment=center
}
\begin{tabular}{m{30mm}SSSS}

Point 1 & -0.0569& 0.0138& -0.1133& 0.1275  \\
Point 2 & -0.1239& {\cellcolor[gray]{0.9}} -0.0189 & {\cellcolor[gray]{0.9}} -0.1092& 0.1662 \\
Point 3 &  0.0209& 0.1032& -0.2188& 0.2428  \\
Point 4 & -0.3064& -3.2415& 0.1353& 3.2588  \\

\end{tabular}
\end{document}

-

You should use the dcolumn package instead to align your numbers automatically on the decimal dots (or other separator):

\documentclass{article}

\usepackage{dcolumn}
%\newcolumntype{d}[1]{D{.}{.}{#1}}% general
\newcolumntype{d}{D{.}{.}{4}}% for four frac. digits
% Syntax: D{<input separator>}{<output/displayed separator>}{<number of frac.digits>}

\begin{document}
\begin{tabular}{m{30mm}dddd}
Point 1 & -0.0569&  0.0138& -0.1133& 0.1275  \\
Point 2 & -0.1239& -0.0189& -0.1092& 0.1662  \\
Point 3 &  0.0209&  0.1032& -0.2188& 0.2428  \\
Point 4 & -0.3064& -3.2415&  0.1353& 3.2588  \\

\end{tabular}
\end{document}


For colors you can use the colortbl package which provides \columncolor.

-

if you have a constant number of digits then there is need for using any special tabular setting, because all digits have in nearly all fonts the same width. However, here is a solution with rccol:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{array,rccol}
\def\CC{\cellcolor{red!30}}

\begin{document}

\begin{tabular}{l *4{R-[.][.]{1}{4}} }
Point 1 & -0.0569 &  0.0138 & -0.1133 & 0.1275       \\
Point 2 & -0.1239 & -0.0189\CC &-0.1092\CC & 0.1662 \\
Point 3 &  0.0209 &  0.1032 & -0.2188 & 0.2428       \\
Point 4 & -0.3064 & -3.2415 &  0.1353 & 3.2588       \\
\end{tabular}

the same with default tabular

\begin{tabular}{l *4r }
Point 1 & -0.0569 &  0.0138 & -0.1133 & 0.1275       \\
Point 2 & -0.1239 & -0.0189\CC &-0.1092\CC & 0.1662 \\
Point 3 &  0.0209 &  0.1032 & -0.2188 & 0.2428       \\
Point 4 & -0.3064 & -3.2415 &  0.1353 & 3.2588       \\
\end{tabular}

\end{document}


-
Thanks guys! That works great! –  user3606 Feb 15 '11 at 15:14