TransWikia.com

pgfmath: Integers without zeros, non-integers with zerofill (and fixed)

TeX - LaTeX Asked on December 4, 2020

enter image description here

I want:
· integers without zeros, e.g. -2 —> -2 (not -2.0000)
· non-integers with zerofill,
e.g. 1.23 —> 1.2300
e.g. 1.2345 –> 1.2345

Hints:
· A pgfmath-solution would be nice (in the difference to siunitx etc.).
· And I need that in a pgfplotstable, but I do not know whether this is a pgfplotstable-specific problem.

What do I have to set?

documentclass{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}

begin{document}
pgfplotstableset{col sep=comma}
section{Actual}
pgfplotstabletypeset[
fixed relative,
zerofill,  precision=4,  % no effect
]{
0, 1,  -2,  1.23,  1.2345
}

section{Target}
pgfplotstabletypeset[string type]{
0, 1,  -2,  1.2300,  1.2345
}
end{document}

One Answer

You can set key /pgfplots/table/@cell content from hook /pgfplots/table/assign cell content in order to define custom output markup for each cell. The argument of assign cell content is the unformatted data read from the input table. pgfmathifisint allows one to run conditional code that depends on whether said data represents an integer.

documentclass{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}

begin{document}
pgfplotstableset{col sep=comma}

pgfplotstabletypeset[
  every column/.style={
    assign cell content/.code={%
      pgfmathifisint{##1}
        {defmyOptions{}}%
        {defmyOptions{fixed, zerofill, precision=4}}%
      expandafterpgfmathprintnumbertoexpandafter[myOptions]
        {pgfretval}{myFormattedValue}%
      pgfplotstableset{@cell content/.expand once=myFormattedValue}%
    },
  }]{
0, 1,  -2,  1.23,  1.2345
}

end{document}

enter image description here

The hook /pgfplots/table/assign cell content is run inside a TeX group, therefore there is no need to manually clean up macros myOptions and myFormattedValue afterwards: this is automatic.

The following is equivalent and shorter, but has a bit more redundancy:

documentclass{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}

begin{document}
pgfplotstableset{col sep=comma}

pgfplotstabletypeset[
  every column/.style={
    assign cell content/.code={%
      pgfmathifisint{##1}
        {pgfmathprintnumberto{pgfretval}{myFormattedValue}}
        {pgfmathprintnumberto[fixed, zerofill, precision=4]
           {pgfretval}{myFormattedValue}}%
      pgfplotstableset{@cell content/.expand once=myFormattedValue}%
    },
  }]{
0, 1,  -2,  1.23,  1.2345
}

end{document}

Also possible, putting all the logic inside each @cell content (which might use a bit more memory, perhaps?):

documentclass{article}
usepackage{pgfplotstable}
pgfplotsset{compat=1.17}

begin{document}
pgfplotstableset{col sep=comma}

pgfplotstabletypeset[
  every column/.style={
    assign cell content/.style={
      @cell content={%
        pgfmathifisint{##1}
          {pgfmathprintnumber{pgfretval}}
          {pgfmathprintnumber[fixed, zerofill, precision=4]{pgfretval}}%
      },
    },
  }]{
0, 1,  -2,  1.23,  1.2345
}

end{document}

Correct answer by frougon on December 4, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP