etoolbox 的条件使用错误?

etoolbox 的条件使用错误?

考虑以下(非最小)示例。

代码

\documentclass{article}

\usepackage{xfp}
\usepackage{etoolbox}

\def\hypEn{1}
\def\hypTo{0}
\def\hypTre{1}
\def\hypFire{4}
\def\hypFem{4}
\def\hypSeks{10}
\def\hypSyv{15}
\def\hypOtte{12}
\def\hypNi{16}
\def\hypTi{3}
\def\hypElleve{2}
\def\hypTolv{1}

\newcommand*\hypKumEn{\hypEn}
\newcommand*\hypKumTo{\fpeval{\hypKumEn+\hypTo}}
\newcommand*\hypKumTre{\fpeval{\hypKumTo+\hypTre}}
\newcommand*\hypKumFire{\fpeval{\hypKumTre+\hypFire}}
\newcommand*\hypKumFem{\fpeval{\hypKumFire+\hypFem}}
\newcommand*\hypKumSeks{\fpeval{\hypKumFem+\hypSeks}}
\newcommand*\hypKumSyv{\fpeval{\hypKumSeks+\hypSyv}}
\newcommand*\hypKumOtte{\fpeval{\hypKumSyv+\hypOtte}}
\newcommand*\hypKumNi{\fpeval{\hypKumOtte+\hypNi}}
\newcommand*\hypKumTi{\fpeval{\hypKumNi+\hypTi}}
\newcommand*\hypKumElleve{\fpeval{\hypKumTi+\hypElleve}}
\newcommand*\hypKumTolv{\fpeval{\hypKumElleve+\hypTolv}}
\newcommand*\hypTotal{\hypKumTolv}

\newcommand*\frekEn{\fpeval{\hypEn/\hypTotal*100}}
\newcommand*\frekTo{\fpeval{\hypTo/\hypTotal*100}}
\newcommand*\frekTre{\fpeval{\hypTre/\hypTotal*100}}
\newcommand*\frekFire{\fpeval{\hypFire/\hypTotal*100}}
\newcommand*\frekFem{\fpeval{\hypFem/\hypTotal*100}}
\newcommand*\frekSeks{\fpeval{\hypSeks/\hypTotal*100}}
\newcommand*\frekSyv{\fpeval{\hypSyv/\hypTotal*100}}
\newcommand*\frekOtte{\fpeval{\hypOtte/\hypTotal*100}}
\newcommand*\frekNi{\fpeval{\hypNi/\hypTotal*100}}
\newcommand*\frekTi{\fpeval{\hypTi/\hypTotal*100}}
\newcommand*\frekElleve{\fpeval{\hypElleve/\hypTotal*100}}
\newcommand*\frekTolv{\fpeval{\hypTolv/\hypTotal*100}}

\newcommand*\frekKumEn{\frekEn}
\newcommand*\frekKumTo{\fpeval{\frekKumEn+\frekTo}}
\newcommand*\frekKumTre{\fpeval{\frekKumTo+\frekTre}}
\newcommand*\frekKumFire{\fpeval{\frekKumTre+\frekFire}}
\newcommand*\frekKumFem{\fpeval{\frekKumFire+\frekFem}}
\newcommand*\frekKumSeks{\fpeval{\frekKumFem+\frekSeks}}
\newcommand*\frekKumSyv{\fpeval{\frekKumSeks+\frekSyv}}
\newcommand*\frekKumOtte{\fpeval{\frekKumSyv+\frekOtte}}
\newcommand*\frekKumNi{\fpeval{\frekKumOtte+\frekNi}}
\newcommand*\frekKumTi{\fpeval{\frekKumNi+\frekTi}}
\newcommand*\frekKumElleve{\fpeval{\frekKumTi+\frekElleve}}
\newcommand*\frekKumTolv{\fpeval{\frekKumElleve+\frekTolv}}
\newcommand*\frekTotal{\frekKumTolv}

\newcommand*\kvartilEn{%
  \ifnumgreater%
    {\frekKumEn}%
    {25}%
    {1}%
    {\ifnumgreater%
      {\frekKumTo}%
      {25}%
      {3}%
      {\ifnumgreater%
        {\frekKumTre}%
        {25}%
        {5}%
        {\ifnumgreater%
          {\frekKumFire}%
          {25}%
          {7}%
          {\ifnumgreater%
            {\frekKumFem}%
            {25}%
            {9}%
            {\ifnumgreater%
              {\frekKumSeks}%
              {25}%
              {11}%
              {\ifnumgreater%
                {\frekKumSyv}%
                {25}%
                {13}%
                {\ifnumgreater%
                  {\frekKumOtte}%
                  {25}%
                  {15}%
                  {\ifnumgreater%
                    {\frekKumNi}%
                    {25}%
                    {17}%
                    {\ifnumgreater%
                      {\frekKumTi}%
                      {25}%
                      {19}%
                      {\ifnumgreater%
                        {\frekKumElleve}%
                        {25}%
                        {21}%
                        {\ifnumgreater%
                          {\frekKumTolv}%
                          {25}%
                          {23}%
                          {}}}}}}}}}}}}
}

\begin{document}

\kvartilEn

\end{document}

输出

这会产生错误

! Missing = inserted for \ifnum.
<to be read again> 
                   .
l.114 \kvartilEn

问题

哪里出了问题?如何修复?

PS 代码可能看起来很奇怪但它是一个更大项目的一部分。:-)

答案1

\ifnumgreater内部使用\ifnum,它只处理整数。你可以改用

\fp_compare:nNnTF {<expr 1>} <rel> {<expr 2>} {<true>} {<false>}

比较浮点值。您可以轻松定义\iffpgreater

\cs_new:Npn \iffpgreater #1 #2
  {
    \fp_compare:nNnTF {#1} > {#2}
      { \use_i:nn }
      { \use_ii:nn }
  }

以下是完整的代码(添加了上面的定义\iffpgreater,并全部替换\ifnumgreater\iffpgreater):

\documentclass{article}

\usepackage{xfp}
\usepackage{etoolbox}

\ExplSyntaxOn
\cs_new:Npn \iffpgreater #1 #2
  {
    \fp_compare:nNnTF {#1} > {#2}
      { \use_i:nn }
      { \use_ii:nn }
  }
\ExplSyntaxOff

\def\hypEn{1}
\def\hypTo{0}
\def\hypTre{1}
\def\hypFire{4}
\def\hypFem{4}
\def\hypSeks{10}
\def\hypSyv{15}
\def\hypOtte{12}
\def\hypNi{16}
\def\hypTi{3}
\def\hypElleve{2}
\def\hypTolv{1}

\newcommand*\hypKumEn{\hypEn}
\newcommand*\hypKumTo{\fpeval{\hypKumEn+\hypTo}}
\newcommand*\hypKumTre{\fpeval{\hypKumTo+\hypTre}}
\newcommand*\hypKumFire{\fpeval{\hypKumTre+\hypFire}}
\newcommand*\hypKumFem{\fpeval{\hypKumFire+\hypFem}}
\newcommand*\hypKumSeks{\fpeval{\hypKumFem+\hypSeks}}
\newcommand*\hypKumSyv{\fpeval{\hypKumSeks+\hypSyv}}
\newcommand*\hypKumOtte{\fpeval{\hypKumSyv+\hypOtte}}
\newcommand*\hypKumNi{\fpeval{\hypKumOtte+\hypNi}}
\newcommand*\hypKumTi{\fpeval{\hypKumNi+\hypTi}}
\newcommand*\hypKumElleve{\fpeval{\hypKumTi+\hypElleve}}
\newcommand*\hypKumTolv{\fpeval{\hypKumElleve+\hypTolv}}
\newcommand*\hypTotal{\hypKumTolv}

\newcommand*\frekEn{\fpeval{\hypEn/\hypTotal*100}}
\newcommand*\frekTo{\fpeval{\hypTo/\hypTotal*100}}
\newcommand*\frekTre{\fpeval{\hypTre/\hypTotal*100}}
\newcommand*\frekFire{\fpeval{\hypFire/\hypTotal*100}}
\newcommand*\frekFem{\fpeval{\hypFem/\hypTotal*100}}
\newcommand*\frekSeks{\fpeval{\hypSeks/\hypTotal*100}}
\newcommand*\frekSyv{\fpeval{\hypSyv/\hypTotal*100}}
\newcommand*\frekOtte{\fpeval{\hypOtte/\hypTotal*100}}
\newcommand*\frekNi{\fpeval{\hypNi/\hypTotal*100}}
\newcommand*\frekTi{\fpeval{\hypTi/\hypTotal*100}}
\newcommand*\frekElleve{\fpeval{\hypElleve/\hypTotal*100}}
\newcommand*\frekTolv{\fpeval{\hypTolv/\hypTotal*100}}

\newcommand*\frekKumEn{\frekEn}
\newcommand*\frekKumTo{\fpeval{\frekKumEn+\frekTo}}
\newcommand*\frekKumTre{\fpeval{\frekKumTo+\frekTre}}
\newcommand*\frekKumFire{\fpeval{\frekKumTre+\frekFire}}
\newcommand*\frekKumFem{\fpeval{\frekKumFire+\frekFem}}
\newcommand*\frekKumSeks{\fpeval{\frekKumFem+\frekSeks}}
\newcommand*\frekKumSyv{\fpeval{\frekKumSeks+\frekSyv}}
\newcommand*\frekKumOtte{\fpeval{\frekKumSyv+\frekOtte}}
\newcommand*\frekKumNi{\fpeval{\frekKumOtte+\frekNi}}
\newcommand*\frekKumTi{\fpeval{\frekKumNi+\frekTi}}
\newcommand*\frekKumElleve{\fpeval{\frekKumTi+\frekElleve}}
\newcommand*\frekKumTolv{\fpeval{\frekKumElleve+\frekTolv}}
\newcommand*\frekTotal{\frekKumTolv}

\newcommand*\kvartilEn{%
  \iffpgreater%
    {\frekKumEn}%
    {25}%
    {1}%
    {\iffpgreater%
      {\frekKumTo}%
      {25}%
      {3}%
      {\iffpgreater%
        {\frekKumTre}%
        {25}%
        {5}%
        {\iffpgreater%
          {\frekKumFire}%
          {25}%
          {7}%
          {\iffpgreater%
            {\frekKumFem}%
            {25}%
            {9}%
            {\iffpgreater%
              {\frekKumSeks}%
              {25}%
              {11}%
              {\iffpgreater%
                {\frekKumSyv}%
                {25}%
                {13}%
                {\iffpgreater%
                  {\frekKumOtte}%
                  {25}%
                  {15}%
                  {\iffpgreater%
                    {\frekKumNi}%
                    {25}%
                    {17}%
                    {\iffpgreater%
                      {\frekKumTi}%
                      {25}%
                      {19}%
                      {\iffpgreater%
                        {\frekKumElleve}%
                        {25}%
                        {21}%
                        {\iffpgreater%
                          {\frekKumTolv}%
                          {25}%
                          {23}%
                          {}}}}}}}}}}}}
}

\begin{document}

\kvartilEn

\end{document}

相关内容