3

Consider the following (non-minimal) example.

Code

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

Output

This produces the error

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

Question

What is wrong and how do I fix it?

P.S. The code might look strange but it's part of a larger project. :-)

6

\ifnumgreater uses \ifnum internally, which only copes with integers. You can use instead

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

to compare floating point values. You can easily define a \iffpgreater:

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

Here's the full code (added the definition of \iffpgreater above, and replaced all \ifnumgreater by \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}
3
  • Of course! I should just have used \ifdimgreater instead of \ifnumgreater and then put pt after the numbers. Thanks for your help! Mar 11 at 12:24
  • 1
    @SvendTveskæg If you don't need too many accurate decimal places, then yes, \ifdimgreater will do (for example, \ifdimgreater{1.012349pt}{1.01234pt} is false) Mar 11 at 14:20
  • Indeed. :-) Oh, and thanks for the nice code, by the way. Mar 11 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.