pgfplotstable:两列合并为一列,但从新列中删除文本(保留数字)

pgfplotstable:两列合并为一列,但从新列中删除文本(保留数字)

问题:这是pgfplotstable:两列合并为一列,其中包含数字数据,但无法四舍五入。不过这一次,我在平均值和标准差列中有一些文本,我想在输出\pgfmathprintnumber新列之前将其删除。我尝试string replace*在代码的各个地方添加,但似乎都不起作用。我怎样才能\pgfmathprintnumber不扫描文本并打印“NaN (NaN)”?

\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&,header=true]{
var & mean & sd \\
a & 95.265 & 11.4801 \\
b & 85.7 & 18.95 \\
c & text & text \\
d & 15 & 5.01 \\
}\means

\pgfplotstabletypeset[
  columns/var/.style={string type},
  columns/mixed/.style={string type,column type=l,column name={Mean (SD)}},
  columns={var, mixed},
  create on use/mixed/.style={
    create col/assign/.code={%
      \edef\entry{\noexpand\pgfmathprintnumber[fixed zerofill, precision=2]{\thisrow{mean}} (\noexpand\pgfmathprintnumber[fixed zerofill, precision=2]{\thisrow{sd}})}%
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    }
  }
]\means
\end{document}

答案1

您可以本地定义 pgf 数学错误命令来设置内部标志(大深度规则)而不是引发错误,然后在决定输出值之前可以测试该标志:在此处输入图片描述

\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&,header=true]{
var & mean & sd \\
a & 95.265 & 11.4801 \\
b & 85.7 & 18.95 \\
c & text & text \\
d & 15 & 5.01 \\
}\means

\makeatletter
\def\pgfmath@error#1#2{\vrule depth 1in}



\def\numorNaN#1{%
\setbox0\hbox{#1}%
\ifdim\dp0<1in\box0 \else\null\fi}
\def\bracketorNaN#1{%
\setbox0\hbox{#1}%
\ifdim\dp0<1in(\box0 )\else\null\fi}

\pgfplotstabletypeset[
  columns/var/.style={string type},
  columns/mixed/.style={string type,column type=l,column name={Mean (SD)}},
  columns={var, mixed},
  create on use/mixed/.style={
    create col/assign/.code={%
      \edef\entry{\noexpand\numorNaN{\noexpand\pgfmathprintnumber[fixed zerofill, precision=2]{\thisrow{mean}}} \noexpand\bracketorNaN{\noexpand\pgfmathprintnumber[fixed zerofill, precision=2]{\thisrow{sd}}}}%
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    }
  }
]\means
\end{document}

相关内容