TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to round p-values in a table to two decimals (for example 0.05), have values less than 0.05 in bold font and have values less than 0.01 to come out as '<0.01' in bold font. Like this (the example is made in Excel):

enter image description here

Question: How do I do this using pgfplotstable package?

Example:

\documentclass{article}

\usepackage{pgfplotstable,tabularx,booktabs}

\begin{document}

\noindent%
\pgfplotstabletypeset[columns={var,p},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=[0.5ex]\bottomrule},
columns/var/.style={string type,column name=Variable},
columns/p/.style={column name=$p$,fixed,fixed zerofill,precision=2,dec sep align},
]{
var p
A   0.0445
B   0.5343
C   0.0000
D   0.0254
E   0.9843
}
\end{document}

Which gives following table with correct rounding:

enter image description here

I manage to get values <0.05 to come out as bold font, but for some reason I do not manage to round the values. Rounding works for the values that are >0.05 (i.e. \pgfmathprintnumber{##1} works, but \textbf{\pgfmathprintnumber{##1}} produces scientific notation without bold font.

\documentclass{article}

\usepackage{pgfplotstable,tabularx,booktabs}

\begin{document}

\noindent%
\pgfplotstabletypeset[columns={var,p},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=[0.5ex]\bottomrule},
columns/var/.style={string type,column name=Variable},
columns/p/.style={column name=$p$,fixed,fixed zerofill,precision=2,
  postproc cell content/.style={
      /pgfplots/table/@cell content/.initial={}{%
       \pgfmathparse{int(less(##1,0.05))}
        \ifnum\pgfmathresult=1
           \textbf{##1}
           \else
           \pgfmathprintnumber{##1}
                \fi
      },
    },
  },
]{
var p
A   0.0445
B   0.5343
C   0.0000
D   0.0254
E   0.9843
}
\end{document}

enter image description here

I also fail to incorporate both bolding and <0.01 into the script. If I run the script with <0.01 condition, I get scientific notation for values that are between 0.01 and 0.05 and correct rounding for values that are >0.05.

\documentclass{article}

\usepackage{pgfplotstable,tabularx,booktabs}

\begin{document}

\noindent%
\pgfplotstabletypeset[columns={var,p},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=[0.5ex]\bottomrule},
columns/var/.style={string type,column name=Variable},
columns/p/.style={column name=$p$,fixed,fixed zerofill,precision=2,
  postproc cell content/.style={
      /pgfplots/table/@cell content/.initial={}{%
       \pgfmathparse{int(less(##1,0.01))}
        \ifnum\pgfmathresult=1
           \textbf{$<$0.01}
           \else
           \pgfmathprintnumber{##1}
                \fi
      },
    },
  },
]{
var p
A   0.0445
B   0.5343
C   0.0000
D   0.0254
E   0.9843
}
\end{document}

enter image description here

Bonus niceness: It would be cool to get the values centered by decimal point, but I guess this is impossible, because <0.01 probably has to come out as a string?

share|improve this question
2  
Please see this one for the bold/regular issue tex.stackexchange.com/questions/119299/… – percusse Jan 27 '14 at 14:29
    
Thanks for the tip. I didn't know that. Both of the following after \ifnum\... produce errors: \mathbf{\pgfmathprintnumber{##1}}, \pgfmathprintnumber{\mathbf{##1}}. Even \pgfmathprintnumber{##1} gives a weird result (scientific notation). There must be something wrong with this line: /pgfplots/table/@cell content/.initial={}{%? – Mikko Jan 27 '14 at 14:52

enter image description here

Working from one of your attempts, and adding the following:

  • bm package for a bolder < symbol
  • conditionals for both p<0.01 and p<0.05

gives a final document of:

\documentclass{article}

\usepackage{pgfplotstable,tabularx,booktabs,bm}

\begin{document}

\noindent%
\pgfplotstabletypeset[columns={var,p},
  every head row/.style={before row=\toprule,after row=\midrule},
  every last row/.style={after row=[0.5ex]\bottomrule},
  columns/var/.style={string type,column name=Variable},
  columns/p/.style={column name=$p$,fixed,fixed zerofill,precision=2,
  postproc cell content/.style={
    /pgfplots/table/@cell content/.initial={}{%
  \pgfmathparse{int(less(##1,0.01))}
  \ifnum\pgfmathresult=1
    $\bm{<0.01}$
  \else
    \pgfmathparse{int(less(##1,0.05))}
    \ifnum\pgfmathresult=1
      $\mathbf{\pgfmathprintnumber[assume math mode=true, fixed, fixed zerofill, precision=2]{##1}}$
    \else
      \pgfmathprintnumber{##1}
    \fi
  \fi
      },
    },
  },
]{
var p
A   0.0445
B   0.5343
C   0.0000
D   0.0254
E   0.9843
}
\end{document}

I'm not certain if having the bold numbers slightly misaligned compared to the normal numbers is a problem or not.

share|improve this answer

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.