根据参数类型调整命令行为

根据参数类型调整命令行为

我的乳胶文档中有以下命令:

\newcommand{\ApplyGradient}[1]{
              \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3}
              \textcolor{black!\PercentColor}{#1}}


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

我使用此代码根据表格中每个单元格的值用灰色阴影为表格着色。它类似于 MS Excel 中的条件格式的工作方式。但是,我遇到了也包含 NaN 的表格。有没有办法调整此代码以使其正确显示文本?

附注:例如:

\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}

取消注释注释行会出现错误。

PPS:我刚刚找到了一个简单的解决方案:将单元格封装在 \multicolumn{1}{r}{NaN} 中即可解决问题:-)

答案1

pgf\pgfmathresult提供检查是否为数字的功能,使用\pgfmathfloatparsenumber\F检查

\pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}

(分为\pgfmathresult“组成部分”):

在此处输入图片描述

\documentclass{article}
\usepackage{pgf,collcell}% http://ctan.org/pkg/{pgf,collcell}

\newcommand{\ApplyGradient}[1]{%
  \pgfmathfloatparsenumber{#1}% Parse float
  \pgfmathfloattomacro{\pgfmathresult}{\F}{\M}{\E}% Break result into components
  \ifnum\F=3\relax% Test flag for sign/number type (3 = NaN)
    \textcolor{red}{#1}%
  \else%
    \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3}%
    \textcolor{black!\PercentColor}{#1}%
  \fi}
\newcolumntype{R}{>{\collectcell\ApplyGradient}{r}<{\endcollectcell}}

\begin{document}
\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{document}

答案2

如果这只是一个用例,那么可能不太实用,但是如果你有相当多的 NaN,pgfplotstable此项检查在内部进行,您只需提供数据和相关样式。

\documentclass{standalone}
\usepackage{pgfplotstable}

\pgfplotsset{compat=1.8,%just to supress warnings
     table/adjust the opacity/.style={
        postproc cell content/.append code={%
             \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\pgfmathresult%
             \ifx\pgfmathresult\empty\relax
             \else%
                 \pgfmathparse{100.0*(\pgfmathresult+0.2)/1.3}
                \begingroup\edef\temp{\endgroup%
                 \noexpand\pgfkeys{/pgfplots/table/@cell content/.add={\noexpand\color{black!\pgfmathresult}}{}}%
                }\temp
             \fi%
        },
    }
}



\begin{document}

\pgfplotstabletypeset[
header=false, %Columns have no header rows
every head row/.style={output empty row},% ALso we don't need a header row
display columns/0/.style={string type,column type=l},% First column is treated as text
display columns/1/.style={column type=r,fixed,adjust the opacity}, 
display columns/2/.style={
    column type=r,
    clear infinite,% Clear the infinities and Nans
    adjust the opacity, % Our style
    empty cells with={NaN},% If empty cell place a NaN
},
display columns/3/.style={column type=r}, 
]{
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{document}

在此处输入图片描述

答案3

您可以将该论点与以下论点进行比较NaN

\documentclass[a4paper,12pt]{report}
\usepackage[english]{babel}

\usepackage{pgf}
\usepackage{collcell}

\newcommand{\ApplyGradient}[1]{%
  \ifnum\pdfstrcmp{#1}{NaN}=0
    #1%
  \else
    \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3}%
    \textcolor{black!\PercentColor}{#1}%
  \fi}
\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}

您可以在条件的“true”分支中添加所需的任何格式,例如,\textcolor{red}{#1}不仅仅是#1

如果你计划将宏与 LuaLaTeX 或 XeLaTeX 一起使用,请执行以下操作

\usepackage{pdftexcmds}
\makeatletter
\newcommand{\ApplyGradient}[1]{%
  \ifnum\pdf@strcmp{#1}{NaN}=\z@
    #1%
  \else
    \pgfmathsetmacro{\PercentColor}{100.0*(#1+0.2)/1.3}%
    \textcolor{black!\PercentColor}{#1}%
  \fi}
\makeatletter

相关内容