12

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.

3
  • 1
    your cnum macro tests tokens but you pass it a box the content of which is opaque to the macro layer of tex. You could use the catchcell package to grab the table cells as a macro Jan 23, 2015 at 1:02
  • Er... so what is the status of this question? What, for that matter, is the question?
    – cfr
    Jan 23, 2015 at 4:25
  • question is : how do I define a newcolumntype that colors negative values in red and positive numbers in blue. will look at catchcell and post it if I can figure it out. thx, david.
    – ivo Welch
    Jan 23, 2015 at 5:26

5 Answers 5

13

It is easy with the collcell package.

Simply define

\newcolumntype{R}{>{\collectcell\cnum}r<{\endcollectcell}}

Note also that \cnum can be simply defined as

\newcommand\cnum[1]{%
  \pgfmathsetmacro{\var}{#1}%
  \pgfmathparse{ifthenelse(\var>=0,1,0)}%
  \ifdim\pgfmathresult pt= 1 pt%
  \textcolor{blue}{#1}%
  \else%
  \textcolor{red}{#1}%
  \fi%
}

MWE

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{collcell}

\newcommand\cnum[1]{%
  \pgfmathsetmacro{\var}{#1}%
  \pgfmathparse{ifthenelse(\var>=0,1,0)}%
  \ifdim\pgfmathresult pt= 1 pt%
  \textcolor{blue}{#1}%
  \else%
  \textcolor{red}{#1}%
  \fi%
}

\newcolumntype{R}{>{\collectcell\cnum}r<{\endcollectcell}}

\begin{document}

\begin{tabular}{RR}
  1.0 & 2 \\
  3 & -4 \\
\end{tabular}

\end{document} 

Output

enter image description here

If the numbers are meant to be typeset in math mode, define the column as

\newcolumntype{R}{>{$\collectcell\cnum}r<{\endcollectcell$}}

and the result will be

enter image description here

3
  • Shouldn't -4 (or at least the minus symbol) be in math mode?
    – Mico
    Jan 23, 2015 at 7:53
  • @Mico I don't know. It isn't in math mode in the OP's example. Jan 23, 2015 at 8:25
  • the solutions are an embarrassment of riches. thank you, karl (and others). I need to do some experimenting on relative typesetting efficiency [esp on pgfplotstable] next to decide which one I should use on long docs...
    – ivo Welch
    Jan 24, 2015 at 6:31
10

This is possible with siunitx too. It provides the option negative-color = <color> which can be used with S column type. Advantage of siunitx include many formatting options for numbers in the document.

\documentclass{scrartcl}
\usepackage{siunitx}
\usepackage{xcolor}
\sisetup{negative-color = red}
\newcolumntype{R}{S[color=blue,negative-color = red]}
\begin{document}

\begin{tabular}{RR}
  1.0 & 2 \\
  3 & -4 \\
\end{tabular}

\num{-15673}
\end{document}

enter image description here

As suggested by Mico, you can define

\newcolumntype{R}[1]{S[color=blue,table-format=#1]}

so that alignment options can be used with R column:

\documentclass{scrartcl}
\usepackage{siunitx}
\usepackage{xcolor}
\sisetup{negative-color = red}
\newcolumntype{R}[1]{S[color=blue,table-format=#1]}
\begin{document}

\begin{tabular}{R{1.1}R{-1.0}}
  1.0 & 2 \\
  3 & -4 \\
\end{tabular}

\num{-15673}
\end{document}

enter image description here

3
  • Given that negative-color = red is set as a global option, is it necessary to repeat it in the definition of the R column type? One could make the definition of R a bit more flexible, say, as \newcolumntype{R}[1]{S[color=blue,table-format=#1]}. This would make it possible to state the two column formats as R[1.1] and R[-1.0], respectively, and enable nice, i.e., tight spacing of the columns.
    – Mico
    Jan 23, 2015 at 8:20
  • @Mico Not necessary to repeat it in R column type. I defined it globally just for using \num. The second suggestion is good. I will modify accordingly (in some time). Thanks. :)
    – user11232
    Jan 23, 2015 at 8:44
  • it falls short on the propagation through macros, I think. for example\begin{tabular}{R{1.1}R{-1.0}} 1.0 & 2 \\ 3 & \bfseries -4 \\ \end{tabular} it's not a big problem and elsewhere, too. just wanted to point it out.
    – ivo Welch
    Jan 28, 2015 at 5:30
7

And this one uses pgfplotstable which provides fonts by sign={<TeX code for positive>}{<TeX code for negative>}

Sample code:

\documentclass{scrartcl}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}
\usepackage{xcolor}
\begin{document}

\pgfplotstabletypeset[
    col sep=&,row sep=\\,
    columns/one/.style={fonts by sign={\color{blue}}{\color{red}}},
    columns/two/.style={fonts by sign={\color{blue}}{\color{red}}},
]{
  one & two \\
  1.0 & 2 \\
  3 & -4 \\
 }

enter image description here

\end{document}
4

If you just want to detect - you don't need the full parsing capabilities of the other packages, just define - to make things red:

enter image description here

\documentclass{article}
\usepackage{array,color}
\newcolumntype{R}{>{$\mathcode`\-"8000\relax\color{blue}}r<$}
\makeatletter
{\catcode`\-\active
\gdef-{\color{red}\mathchar"2200\relax}}
\makeatletter

\begin{document}

\begin{tabular}{R R}
  -1 & 2 \\
  3 & -4 \\
\end{tabular}

\end{document}
2
  • thx, david. actually, this is almost comically simple and it will suffice in 99 out of 100 cases. (in 1 out of 100, I may have some cells that start with \bfseries or some macro here that won't propagate through.) for curiosity---and I presume no---can such a really simple macro be extended to use whitespace (perl regex \s+) instead of '&' and \n as \\ , too, ala pgfplotstable?
    – ivo Welch
    Jan 24, 2015 at 6:26
  • @ivoWelch I'm not sure I understand the question but I assume that the answer is yes. This isn't using & and \\ in any essential way it just defines - to change the color then relies on tex grouping in this case the groups are proivided by table cells, but explicit {} groups would work and if you have a space delimited macro that is grouping cells that would work too Jan 24, 2015 at 9:36
1

red if it starts with a minus, blue if it starts with a different catcode 12 character, black otherwise.

\documentclass{article}
\usepackage{tabularx,xcolor}
\def\testnext#1#2{#1\ifx-#2\color{red}\else\ifcat#20\color{blue}\else\fi\fi#2}
\begin{document}
\newcolumntype{R}{>{\testnext}r<{}}
\begin{tabular}{R R}
one & two \\
  -1 & 2 \\
  3 & -4 \\
\end{tabular}
\end{document}
\end{document}

enter image description here

1
  • 1
    I made it a little more flexible: newcommand{\negnumformat}{\color{red}} % -1 \newcommand{\posnumformat}{\color{blue}} % +1 \newcommand{\numformat}{\color{black}} % 1 \newcommand{\txtformat}{\color{black}} % A \def\testnext#1#2{#1\ifx-#2\negnumformat$#2$\else\ifx+#2\posnumformat$#2$\else\ifcat#20\numformat$#2$\else\txtformat#2\fi\fi\fi}
    – ivo Welch
    Oct 31, 2018 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.