I believe numprint.sty
is not working. at least the manual states that Printing negative numbers in red does not work. Only the minus sign is printed in red. When was this bug introduced? Some earlier sx
solutions that are based on a gobble of the minus sign don't propagate through macros. however, an adaptation of tikz
(as explained in How to test if a number is negative) works.
\documentclass{scrartcl}
\usepackage{tikz}
\newbox\boxcca
\newcommand\ifPositif[3]{
\begingroup
\pgfmathsetmacro{\var}{#1}
\pgfmathparse{ifthenelse(\var>=0,1,0)}
\ifdim\pgfmathresult pt= 1 pt
\color{blue}{#2}
\else
\color{red}{#3}
\fi
\endgroup
}
\newcommand{\cnum}[1]{\ifPositif{#1}{#1}{#1}}
\begin{document}
\cnum{1.0} %% works
\cnum{-3} %% works
\newcommand{\useinmacro}[1]{\cnum{#1}} %% works
\useinmacro{22} %% works
\useinmacro{-23} %% works
\begin{lrbox}{\boxcca} -123.2 \end{lrbox} %% works
\end{document}
unfortunately, I could not get this to work in my tabulars with
\newcolumntype{R}{>{\begin{lrbox}{\boxcca}} r <{\end{lrbox}\cnum{\boxcca}}}
\begin{tabular}{R R}
-1 & 2 \\
3 & -4 \\
\end{tabular}
\end{document}
I tried many variations, and I would guess my error is obvious to the eye of the expert and easy to fix. advice (as always) appreciated.
regards,
PS: til tantau seems like a genius to me. how in the world did he manage to code tikz into tex?? a moment ago, I also discovered christian feuersaenger's pgfplotstable and marco's answer in Automatic coloring of numbers according to size . this may be adaptable, too. except for its long names, pgfplotstable looks insanely great—it has the ability to make whitespace or '|' the tabular separator! wow. I presume pgfplotstable means giving up booktabs, array, and a couple of other tabular enhancements that I have become fond of, but it seems worth it. of course, if I do this, the above question is probably obsolete.
Best Answer
It is easy with the
collcell
package.Simply define
Note also that
\cnum
can be simply defined asMWE
Output
If the numbers are meant to be typeset in math mode, define the column as
and the result will be