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: 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?

share|improve this question
add comment

1 Answer 1

up vote 8 down vote accepted

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}
share|improve this answer
    
Awesome, thanks so much! I was getting frustrated with that. –  Luke Oct 30 '12 at 19:58
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.