3

I have a csv file conaining differently precise numbers. Now i want to plot them with precision=1. The reader should see that some values are integers, so the respective number should be shown without decimal places, but numbers that are rounded to a zero fractional digit should be shown with a trailing 0.

Here the MWE:

\documentclass[12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}

\begin{filecontents}{data.csv}
81
81.0234
81.4520
\end{filecontents}

\pgfplotstabletypeset[ 
      col sep=comma,
      zerofill,
      precision=1,  
    ]{data.csv}
\end{document}

output:

enter image description here

Is there a general number format in pfgplotstable that only truncates the fractional digit from the first 81 to indicate the missing precision in the raw data?

I tested different number formats from the PgfplotsTable manual but didn't find a solution.

fmetz
  • 555

2 Answers2

3

Alternatively, you can catch the entries and format them independently depending on whether they are integers or not.

\documentclass{standalone}
\usepackage{pgfplotstable}

\pgfplotstableset{
  int or print/.style={
    preproc cell content/.append code={%
      \pgfmathifisint{##1}{%
        \pgfmathtruncatemacro{\pgfretval}{\pgfretval}%        
        }{%
        \pgfkeys{/pgf/number format/.cd,#1}%
        }%
      \pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfretval}%
    }%
  }
}

\begin{document}
\pgfplotstabletypeset[ 
    col sep=comma,
    header=false,
    every head row/.style={output empty row},
    int or print={sci,sci zerofill,precision=7}%Choose what the float should look like
    ]{
81
81.0234
81.4520
81.0000
81.00000001 % See the precision
}
\end{document}

enter image description here

percusse
  • 157,807
2

The macro which detect integer is\pgfmathprintnumber@INT@DETECT defined like this

\def\pgfmathprintnumber@INT@DETECT#1{%
    \pgfmathfloatparsenumber{#1}%
    \pgfmathfloat@to@FMeE@style\pgfmathresult
    \expandafter\pgfmathprintnumber@INT@DETECT@issci\pgfmathresult\relax
} 

If the input number is an integer no period is displayed at all. If not, the scientific format is chosen.

To change this and use fixed format instead of scientific you can change the macro \pgfmathprintnumber@INT@DETECT@issci which is defined like this

\def\pgfmathprintnumber@INT@DETECT@issci#1#2e#3\relax{%
    \begingroup
    \ifnum#1<3\relax
        \pgfmathfloatcreate{#1}{#2}{#3}%
        \pgfmathfloattofixed{\pgfmathresult}%
        \let\pgfmathfloat@round@precision@=\pgfmathfloat@round@precision
        \def\pgfmathfloat@round@precision{6}%
        \expandafter\pgfmathroundto\expandafter{\pgfmathresult}%
        \let\pgfmathfloat@round@precision=\pgfmathfloat@round@precision@
        \ifpgfmathfloatroundhasperiod
            \pgfmathprintnumber@SCI@issci#1#2e#3\relax
        \else
            \expandafter\pgfmathprintnumber@fixed@style\expandafter{\pgfmathresult}#1#2e#3\relax
        \fi
    \else
        \pgfmathfloatrounddisplaystyle#1#2e#3\relax%
    \fi
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}

With this

\def\pgfmathprintnumber@INT@DETECT@issci#1#2e#3\relax{%
    \begingroup
    \ifnum#1<3\relax
        \pgfmathfloatcreate{#1}{#2}{#3}%
        \pgfmathfloattofixed{\pgfmathresult}%
        \let\pgfmathfloat@round@precision@=\pgfmathfloat@round@precision
        \def\pgfmathfloat@round@precision{6}%
        \expandafter\pgfmathroundto\expandafter{\pgfmathresult}%
        \let\pgfmathfloat@round@precision=\pgfmathfloat@round@precision@
        \ifpgfmathfloatroundhasperiod
        \expandafter\pgfmathroundtozerofill\expandafter{\pgfmathresult}
        \expandafter\pgfmathprintnumber@fixed@style\expandafter{\pgfmathresult}#1#2e#3\relax
        \else
            \expandafter\pgfmathprintnumber@fixed@style\expandafter{\pgfmathresult}#1#2e#3\relax
        \fi
    \else
        \pgfmathfloatrounddisplaystyle#1#2e#3\relax%
    \fi
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}

And your code become

\documentclass[12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}


\makeatletter
\def\pgfmathprintnumber@INT@DETECT@issci#1#2e#3\relax{%
    \begingroup
    \ifnum#1<3\relax
        \pgfmathfloatcreate{#1}{#2}{#3}%
        \pgfmathfloattofixed{\pgfmathresult}%
        \let\pgfmathfloat@round@precision@=\pgfmathfloat@round@precision
        \def\pgfmathfloat@round@precision{6}%
        \expandafter\pgfmathroundto\expandafter{\pgfmathresult}%
        \let\pgfmathfloat@round@precision=\pgfmathfloat@round@precision@
        \ifpgfmathfloatroundhasperiod
        \expandafter\pgfmathroundtozerofill\expandafter{\pgfmathresult}
        \expandafter\pgfmathprintnumber@fixed@style\expandafter{\pgfmathresult}#1#2e#3\relax
        \else
            \expandafter\pgfmathprintnumber@fixed@style\expandafter{\pgfmathresult}#1#2e#3\relax
        \fi
    \else
        \pgfmathfloatrounddisplaystyle#1#2e#3\relax%
    \fi
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}
\makeatother


\begin{document}

\begin{filecontents}{data.csv}
81
81.0234
81.4520
\end{filecontents}

\pgfplotstabletypeset[ 
      col sep=comma,
      precision=1,
      int detect,
      ]{data.csv}    


\end{document}

Result

enter image description here

Salim Bou
  • 17,021
  • 2
  • 31
  • 76