4

Question: This is a followup up to pgfplotstable: Two columns into one with numeric data, but can't round. Except this time, I have some text throughout the mean and sd column and would like to remove it before the \pgfmathprintnumber outputs the new column. I have tried including a string replace* throughout various places in the code, but none seems to work. How do I get \pgfmathprintnumber to not scan the text and print the '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}
David Carlisle
  • 757,742

1 Answers1

5

You can locally define the pgf math error command to set an internal flag (large depth rule) instead of raising an error, and then can test for that flag before deciding to output the value:enter image description here

\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}
David Carlisle
  • 757,742