# How to treat with dcolumn tabular format and colored values?

I found here this trick for formated text coloring for use with dcolumn package. However, it seems roughly redefine the \DC@endright statement and gives two wrong behaviours with values which have more than one character:

1) bad vertical aligment

2) bad row color filling with \rowcolor command

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{dcolumn}

\makeatletter
\def\DC@endright{\hfil\egroup\@dcolcolor\box\z@\box\tw@\dcolreset} \def\dcolcolor#1{\gdef\@dcolcolor{\color{#1}}} \def\dcolreset{\dcolcolor{black}} \dcolcolor{black} \makeatother \begin{document} \begin{tabular}{|*{3}{D{.}{,}{1}|}} \rowcolor{gray!20} 1 & 2 & 3 \\ \rowcolor{gray!50} -1 & 2 & 3 \\ \rowcolor{gray!20} 1 & 22 & 3 \\ \rowcolor{gray!50} 1 & 2 & \dcolcolor{red}3.3 \\ \end{tabular} \end{document}  As you can test, these issues disappear if you comment the trick lines. Nevertheless, I'd like to patch them in order to coloring values in cells. note: the vertical rules are just to control what happens. • This question maybe should be a comment for the mentionated entry, but my poor reputation don't let me do it. – e_moro May 15 '19 at 21:12 • Would switching to the siunitx package be an option for you? – leandriis May 15 '19 at 21:20 • Off-topic: Don't load the xcolor and colortbl packages independently of each other. Instead, run the single instruction \usepackage[table]{xcolor}. – Mico May 22 '19 at 11:09 • @leandriis As you suggest, I tried siunitx. It works perfectly with this MWE. But unfortunately my final goal is a bit more complicated: the table must be generated using a loop over columns. And this is the 'S' column type limitation: it can't embed the loop command, like 'D' column type (dcolumn package) do. (please see my another question [tex.stackexchange.com/questions/489747/… – e_moro May 22 '19 at 13:50 • @e_moro - I don't follow you when you say that the S column type cannot be embedded in a loop command, whereas the D column type can. Can you elaborate on that? – Mico May 23 '19 at 5:53 ## 2 Answers Instead of using the dcolumn package, I suggest you work with the siunitx package. The siunitx package provides, among many other things, the S column type, which allows aligning numbers on the (implicit or explicit) decimal markers. \documentclass{article} \usepackage[spanish]{babel} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{lmodern,dcolumn} \usepackage[table,svgnames]{xcolor} %% New: \usepackage{siunitx} \sisetup{output-decimal-marker={,}} \usepackage{etoolbox} % for "\robustify" macro \robustify{\cellcolor} \begin{document} \begin{tabular}{|*{3}{D{.}{,}{1}|}} \rowcolor{gray!20} 1 & 2 & 3 \\ \rowcolor{gray!50} -1 & 2 & 3 \\ \rowcolor{gray!20} 1 & 22 & 3 \\ %\rowcolor{gray!20} 1 & 2 & \dcolcolor{red}3.3 \\ \rowcolor{gray!20} 1 & 2 & 3.3 \\ \end{tabular} \smallskip vs.\par\smallskip \begin{tabular}{|S[table-format=-1.0] S[table-format=2.0] S[table-format=1.1]|} \rowcolor{gray!20} 1 & 2 & 3 \\ \rowcolor{gray!50} -1 & 2 & 3 \\ \rowcolor{gray!20} 1 & 22 & 3 \\ \rowcolor{gray!20} 1 & 2 & \cellcolor{pink}3.3 \\ \rowcolor{gray!20} 1 & 2 & 3.3 \\ \end{tabular} \end{document}  • Yes, the 'S' column type is a good idea and also solves the values coloring. However, as I explained in a previous commentary above, this approach has its own issues for my whole problem. (That is the limitation of the use of MWEs) – e_moro May 22 '19 at 13:46 Since v1.03 dcolumn package add the new notation D{.}{.}{n.m}, where n,m are the number of places to the left and to the right of the decimal separator. It solves the mentioned alignment problem and seems to be the simplest solution. So, the siunitx package is not necessary. Furthermore, dcolumn is compatible with datatool and the previously cited \excolloop command. The following code show the different approaches: \documentclass{article} \usepackage[table]{xcolor} \usepackage{siunitx,dcolumn,datatool} % first example configuration \sisetup{output-decimal-marker = {,}} % optional for countries with a decimal comma % needed in second example \makeatletter \def\DC@endright{\hfil\egroup\@dcolcolor\box\z@\box\tw@\dcolreset}
\def\dcolcolor#1{\gdef\@dcolcolor{\color{#1}}}
\def\dcolreset{\dcolcolor{black}}
\dcolcolor{black}
\makeatother

% data for the third example
\begin{filecontents*}{datos.csv}
1,2,3
-1,2,3
1,22,3
1,2,3.3
\end{filecontents*}

\begin{document}

% first example
\begin{tabular}{|*{3}{S[table-format=2.1]|}}
\rowcolor{gray!20}  1 & 2 & 3 \\
\rowcolor{gray!50}  -1 & 2 & 3 \\
\rowcolor{gray!20}  1 & 22 & 3 \\
\rowcolor{gray!50}  1 & 2 & \color{red}3.3 \\
\end{tabular}

\bigskip

% second example
\begin{tabular}{|*{3}{D{.}{,}{2.1}|}}
\rowcolor{gray!20}  1 & 2 & 3 \\
\rowcolor{gray!50}  -1 & 2 & 3 \\
\rowcolor{gray!20}  1 & 22 & 3 \\
\rowcolor{gray!50}  1 & 2 & \dcolcolor{red}3.3 \\
\end{tabular}

\bigskip

% third example
\DTLloaddb[noheader]{tabla}{datos.csv}
\begin{tabular}{|*{3}{D{.}{,}{2.1}|}}
\rowcolor{gray!20}
\DTLforeach*{tabla}{\one=Column1,\two=Column2,\three=Column3}{%
\one & \two & \DTLifeq{\three}{3.3}{\dcolcolor{red}}{}\three%
\DTLiflastrow{}{\\ \DTLifoddrow{\rowcolor{gray!50}}{\rowcolor{gray!20}}}%
}\\
\end{tabular}

\end{document}