8

I want to typeset a table where certain numbers are going to appear red. In order to achieve the correct alignment I am using the dcolumn package. The problem is that the parsing that dcolumn does, prevents the correct appliance of color.

In the following example, in the third row, only the "1" appears red. The dot and the "32" appears black. Using \textcolor results in a syntactical mistake.

I tried using siunitx, but unfortunately, since I am producing programmatically hundreds of tables, the compilation time increased too much (more than a minute)

So the question is, how can you color a number belonging in a dcolumn?

\documentclass[]{article}

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

\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\begin{tabu}{cd{2}}
note & 1.32\\
\color{red}note & 1.32 \\
\color{red}note & \color{red}1.32 \\
% note & 1.32\\
% \textcolor{red}{note} & 1.32 \\
% \textcolor{red}{note} & \textcolor{red}{1.32} \\  % results in a compilation error.
\end{tabu}

\end{document}

2 Answers 2

7

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}

2
  • 3
    Yes, sorry about that. I probably should have defined dcolumn to try harder to use the same typesetting state from the first box in any later boxes. Trouble would be knowing where to stop, colours, the math font family setup, sizes, user defined macros from \newcommand? Probably such a feature couldn't be done in a compatible way, but I add this comment should anyone be making an improved xxxdcolumn package and is looking for ideas... It would be nice (and probably possible) to make \mathsf{\color{red} 123.456} change the font and colour without messing up the alignment. Jan 30, 2012 at 11:58
  • 1
    @DavidCarlisle: Thanks for the comment. I think siunitx can handle such a feature much easier by using the LaTeX3-Syntax. Maybe Joseph will provide this. Jan 30, 2012 at 12:02
3

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.

1
  • Unfortunately, as I have already mentioned in my question, siunitx is not an option since it increases unacceptably the compilation time... But thanks anyway :)
    – pmav99
    Jan 30, 2012 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .