1

On page 1/2 of the xfp package we read:

  • The floating point expressions may comprise:Comparison operators: x < y, x <= y, x >? y, x ! = y etc. However they did not show an example. Below I have an example in which I use \fpeval{\x<\y } to make a decision in a conditional command:

    \documentclass{article}
    \usepackage{xfp, ifthen}
    \begin{document}
    \edef\x{6.25}
    \edef\y{-2}
    \noindent $x=\fpeval{\x}$.\\
     $y=\fpeval{\y}$.\\
    \ifthenelse{\fpeval{\x<\y }}
     {$x\lt y$}
     {$x\ge y$}
     \end{document}
    

But got an error:

! Missing = inserted for \ifnum.

\relax l.10 \ifthenelse{\fpeval{\x<\y }}

Do you know how to fix it?

Aria
  • 1,523

1 Answers1

1

The first argument to \ifthenelse must contain a test (by default a test on integer equality).

You could do \ifthenelse{\fpeval{\x<\y}=1}{...}{...} which would return true if \x is actually less than \y.

Example:

\documentclass{article}
\usepackage{xfp,ifthen}

\begin{document}

\def\x{6.25} \def\y{-2}

\ifthenelse{\fpeval{\x<\y}=1}{TRUE}{FALSE} (should be F)

\ifthenelse{\fpeval{\y<\x}=1}{TRUE}{FALSE} (should be T)

\end{document}

enter image description here

With more flexible (and fully expandable) code that I already suggested you

\documentclass{article}
\usepackage{xfp,xparse}

\usepackage{xparse}

\ExplSyntaxOn \NewExpandableDocumentCommand{\xifthenelse}{mmm} { \bool_if:nTF { #1 } { #2 } { #3 } }

\cs_new_eq:NN \numtest \int_compare_p:n \cs_new_eq:NN \oddtest \int_if_odd_p:n \cs_new_eq:NN \fptest \fp_compare_p:n \cs_new_eq:NN \dimtest \dim_compare_p:n \cs_new_eq:NN \deftest \cs_if_exist_p:N \cs_new_eq:NN \namedeftest \cs_if_exist_p:c \cs_new_eq:NN \eqdeftest \token_if_eq_meaning_p:NN \cs_new_eq:NN \streqtest \str_if_eq_p:ee \cs_new_eq:NN \emptytest \tl_if_blank_p:n \prg_new_conditional:Nnn \xxifthen_legacy_conditional:n { p,T,F,TF } { \use:c { if#1 } \prg_return_true: \else: \prg_return_false: \fi: } \cs_new_eq:NN \boolean \xxifthen_legacy_conditional_p:n \ExplSyntaxOff

\begin{document}

\def\x{6.25} \def\y{-2}

\xifthenelse{\fptest{\x<\y}}{TRUE}{FALSE} (should be F)

\xifthenelse{\fptest{\y<\x}}{TRUE}{FALSE} (should be T)

\end{document}

egreg
  • 1,121,712