如何仅截断 pgfplotstable 整数条目,但不截断四舍五入的浮点数?

如何仅截断 pgfplotstable 整数条目,但不截断四舍五入的浮点数?

我有一个包含不同精度数字的 csv 文件。现在我想用 绘制它们precision=1。读者应该看到有些值是整数,因此相应的数字应该不带小数位显示,但四舍五入为零小数位的数字应该在尾部显示0

这里是 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}

输出:

在此处输入图片描述

pfgplotstable 中是否存在通用数字格式,仅从第一位截断小数位81以指示原始数据中缺少的精度?

我测试了不同的数字格式PgfplotsTable 手册但没有找到解决方案。

答案1

检测整数的宏\pgfmathprintnumber@INT@DETECT定义如下

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

如果输入的数字是整数,则不显示句点。如果不是,则选择科学计数法格式。

\pgfmathprintnumber@INT@DETECT@issci要改变这种情况并使用固定格式而不是科学格式,您可以更改如下定义的宏

\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
}

有了这个

\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
}

你的代码变成

\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}

结果

在此处输入图片描述

答案2

或者,您可以捕获条目并根据它们是否为整数来独立地格式化它们。

\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}

在此处输入图片描述

相关内容