1

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

enter image description here

\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.

e_moro
  • 888
  • 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
  • 2
    Would switching to the siunitx package be an option for you? – leandriis May 15 '19 at 21:20
  • 1
    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 [https://tex.stackexchange.com/questions/489747/loops-on-columns-inside-tabular-environment-and-multicolumn] – 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 05:53
  • @Mico, as you can see in [https://tex.stackexchange.com/questions/489747/loops-on-columns-inside-tabular-environment-and-multicolumn], Skillmon developed a new command called \colloop, which is a loop on table columns. For my purpuose, I need both features: colored values and loops. Here is the issue: \colloop works well in a table like \tabular{3{D}}, but it doesn't in \tabular{3{S}}. For this reason I can't use siunitx. On the other hand, D column type (from dcolumn package) doesn't color the values ​​properly and needs to be patched. This was discussed in a cited link. – e_moro May 23 '19 at 13:55
  • @e_moro - If the ability to use some column type in a data loop is crucially important, it would have been nice if you had mentioned this piece of information up front. FWIW, the answer by @Skillmon that you reference does not contain numeric data. Since you do not indicate how you have adapted @Skillmon's code for your purposes, it's not possible to evaluate your claim that the D column type works correctly whereas the S column type does not. – Mico May 23 '19 at 15:04
  • @Mico: Since I'd confirmed that dcolumn is compatible with datatool and \colloop, I reduced my issue to the MWE shown in the question. In that way, the title of the question talks precisely about dcolumn. In any case, I apologize for the misunderstanding. – e_moro May 23 '19 at 22:13

2 Answers2

2

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.

enter image description here

\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}
Mico
  • 506,678
  • 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
0

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}

enter image description here

e_moro
  • 888