为 \pgfmathaccuma 分配初始值

为 \pgfmathaccuma 分配初始值

\pgfmathaccuma我无法将和最初赋值为零\pgfmathaccumb。它给出以下错误:

! Missing number, treated as zero.

然后我按s跳过。但是,\pgfmathaccumb始终是空的!我想将其初始值设置为零。

这是我的 MWE:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data.txt}
index   mmtype
0       0
1       0
2       0
3       1
4       0
\end{filecontents}

\begin{document}
\pgfplotstableread{data.txt}\origtable

\pgfplotstablenew[]{6}\newtable

\pgfplotstablecreatecol[
create col/assign/.code={\pgfkeyslet{/pgfplots/table/create col/next content}\pgfplotstablerow},
]{rowid}\newtable

\pgfplotstablecreatecol[
create col/assign/.code={%
\ifx\pgfmathaccuma\pgfutil@empty
\pgfmathparse{0}%
\let\pgfmathaccuma=\pgfmathresult
\fi
\ifx\pgfmathaccumb\pgfutil@empty
\pgfmathparse{0}%
\let\pgfmathaccumb=\pgfmathresult
\fi
\pgfmathtruncatemacro\orgrow{\pgfplotstablerow-\pgfmathaccuma}%
b:\pgfmathaccumb|
\ifx\pgfmathaccumb=0
    \pgfplotstablegetelem{\orgrow}{mmtype}\of\origtable%
    \ifx\pgfplotsretval=1
        01
        \pgfmathparse{\pgfmathaccuma+1}%
        \let\pgfmathaccuma=\pgfmathresult%
        \pgfmathparse{1}%
        \let\pgfmathaccumb=\pgfmathresult%
    \fi
    \pgfkeyslet{/pgfplots/table/create col/next content}\orgrow%
\else
    \orgrow:1
    \pgfmathparse{0}%
    \let\pgfmathaccumb=\pgfmathresult%  
    \pgfkeyslet{/pgfplots/table/create col/next content}\orgrow%
\fi
},]{mmtypenewrow}\newtable

\pgfplotstabletypeset\newtable

\end{document}

index我的目标是如果它mmtype等于 1 则重复。例如,当输入数据如下时:

index   mmtype
0       0
1       0
2       0
3       1
4       0

输出数据必须是:

mmtypenewrow    mmtype
0               0
1               0
2               0
3               1
3               1
4               0

答案1

您正在使用\pgfutil@empty不能使用的地方:在文档中,该字符@不能通常用于制作控制序列,因此结果是与(未定义)\ifx进行比较并且测试为假。\pgfmathaccuma\pgfutil

如何跳过这个问题?要么将代码放在和之间\makeatletter\makeatother要么定义不同的测试:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{data.txt}
index   mmtype
0       0
1       0
2       0
3       1
4       0
\end{filecontents}

%%% Define a macro for testing against `\pgfutil@empty`    
\makeatletter
\def\checkforempty#1{TT\fi\ifx#1\pgfutil@empty}
\makeatother

\begin{document}
\pgfplotstableread{data.txt}\origtable

\pgfplotstablenew[]{6}\newtable

\pgfplotstablecreatecol[
create col/assign/.code={\pgfkeyslet{/pgfplots/table/create col/next content}\pgfplotstablerow},
]{rowid}\newtable

\makeatletter
\pgfplotstablecreatecol[
create col/assign/.code={%
\if\checkforempty\pgfmathaccuma
  \pgfmathparse{0}%
  \let\pgfmathaccuma=\pgfmathresult
\fi
\ifx\checkforempty\pgfmathaccumb
  \pgfmathparse{0}%
  \let\pgfmathaccumb=\pgfmathresult
\fi
\pgfmathtruncatemacro\orgrow{\pgfplotstablerow-\pgfmathaccuma}%
b:\pgfmathaccumb|
\ifx\pgfmathaccumb=0
    \pgfplotstablegetelem{\orgrow}{mmtype}\of\origtable%
    \ifx\pgfplotsretval=1
        01
        \pgfmathparse{\pgfmathaccuma+1}%
        \let\pgfmathaccuma=\pgfmathresult%
    \fi
    \pgfkeyslet{/pgfplots/table/create col/next content}\orgrow%
\else
    \orgrow:1
    \pgfmathparse{0}%
    \let\pgfmathaccumb=\pgfmathresult%  
    \pgfkeyslet{/pgfplots/table/create col/next content}\orgrow%
\fi
},]{mmtypenewrow}\newtable

\pgfplotstabletypeset\newtable

\end{document}

所以你可以

\if\checkforempty\pgfmathaccuma

代码将会完成您想要做的事情。

相关内容