Sunday, 26 May 2013

Adjusting command behavior based on argument type

Adjusting command behavior based on argument type

I have a following command in my latex document:
\newcommand{\ApplyGradient}[1]{
              \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3}
              \textcolor{black!\PercentColor}{#1}}


\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}
I use this code to color my tables with shades of gray depending on the value within each cell of the table. It is similar to how conditional formatting works in MS Excel. However, I encountered table that also contains NaNs. Is there a way to adapt this code so that it would display text correctly?
PS: Example:
\documentclass[a4paper,12pt]{report}
\usepackage[english]{babel}  

\usepackage{pgf}   
\usepackage{collcell}

\newcommand{\ApplyGradient}[1]{%
    \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3}
    \textcolor{black!\PercentColor}{#1}}
\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}

\begin{document}
\begin{table}[htbp]
\begin{tabular}{lRRr}
p2p & 0.08 & 0.74 & 10\\
p2p like dest & 0.67 & 0.25 & 185\\
%scan sql & 0.01 & NaN & 23\\
skype s.d. & 0.67 & 0.71 & 80\\
\end{tabular}
\end{table}
\end{document}
Uncommenting the commented line gives the error.
PPS: I just found an easy solution: encapsulating the cell in \multicolumn{1}{r}{ NaN} solves the problem :-)

No comments:

Post a Comment