Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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}
share|improve this answer
    
Yup, works exactly how I want it to look like. So much for me to still learn... Thanks. –  Luke Nov 2 '12 at 21:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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