7

Question: Relates to the question posed here. I have made one column from two, but I would like to round the numbers off from a precision of 4 to 2. How do I do that?

Here is the MWE:

\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&,header=true]{
var & mean & sd \\
x & 95.265 & 11.4801 \\
y & 85.7 & 18.95 \\
z & 15 & 5.01 \\
}\means

\pgfplotstabletypeset[
  columns/var/.style={string type},
  columns/mean/.style={fixed zerofill,precision=2},
  columns/sd/.style={fixed zerofill,precision=2},
  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{\thisrow{mean} (\thisrow{sd})}%
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
    }
  }
]\means    \end{document}

The column "Mean (SD)" in the output is in the raw data format, even though I had specified the precision in the columns of "mean" and "sd". How do I get it to round off appropriately with zerofill?

0

1 Answer 1

10

In your code, you're setting the number printing options for the two output columns mean and sd, but you're not actually printing them (you're only printing the columns var and mixed). Within the mixed column, you're not passing the numbers through the number formatting routine, but inserting them directly from the source table. Replacing \thisrow{mean} with \noexpand\pgfmathprintnumber[fixed zerofill, precision=2]{\thisrow{mean}} and \thisrow{sd} with \noexpand\pgfmathprintnumber[fixed zerofill, precision=2]{\thisrow{sd}} will lead to the desired result:

\documentclass{standalone}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[row sep=\\,col sep=&,header=true]{
var & mean & sd \\
x & 95.265 & 11.4801 \\
y & 85.7 & 18.95 \\
z & 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
  • Awesome, thanks so much! I was getting frustrated with that. Oct 30, 2012 at 19:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .