Colored text with dcolumn

I can provide an approach by redefining the internal definition of the output.

The contents of a dcolumn is saved in two boxes and so you have to set the color before the output. Additional you must reset the color.

The example below provides a new command \dcolcolor to set the color of a cell defined by dcolumn. The usage is simple (\dcolcolor has one mandatory argument):

\dcolcolor{<color>}

Here the example:

\documentclass[]{article}

\usepackage{xcolor}
\usepackage{tabu}
\usepackage{dcolumn}

\newcolumntype{d}[1]{D{.}{.}{#1}}
\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{tabu}{cd{2}}
note & 1.32\\
\color{red}note & 1.32 \\
note &\dcolcolor{red} 1.32 \\
 note & 1.32\\
 note & \dcolcolor{red}1.32\\
 \textcolor{red}{note} & 1.32 \\
\end{tabu}
\end{document}


You may want to also check out the formatting capabilities of the siunitx package. The package provides, among many other things, a column type labeled S. With this package loaded instead of dcolumn, your MWE would like this this:

\documentclass[]{article}
\usepackage{xcolor}
\usepackage{tabu}
\usepackage{siunitx}

\begin{document}
\begin{tabu}{cS[table-format=1.2]}
note & 1.32\\
\color{red}note & 1.32 \\
\color{red}note & \color{red}1.32 \\
\textcolor{red}{note} & 1.32 \\
\textcolor{red}{note} & \textcolor{red}{1.32} \\
\end{tabu}
\end{document}

enter image description here

Observe that the package and its S column type have no problems with either \color or \textcolor.

Tags:

Color

Dcolumn