0

if i use

\FPeval{\one}{\csname test1\endcsname}

everything is fine. If i use

\FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname}

in \ifthenelse, then i get en error.

\documentclass[10pt,german]{article}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel} 
\usepackage[utf8]{inputenc}  
\usepackage{pgffor} %foreach    
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp   -> FPeval
\usepackage{fp} 
\usepackage{ifthen,changepage}

 \expandafter\xdef\csname test1\endcsname{5} 
 \expandafter\xdef\csname test2\endcsname{4} 

\begin{document}


 %\FPeval{\one}{\csname test1\endcsname} <- this works
 \FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname} % <- this doesnt work
 \FPeval{\two}{\csname test2\endcsname}

 \ifthenelse{\one > \two}{case 1 }{case 2}


\end{document}

someone have an idea?

  • \FPeval creates float numbers, the comparision in ifthenelse is for integers. You will have to convert the numbers first, or compare the numbers as dimension, or use another test. – Ulrike Fischer Apr 18 at 8:05
  • and how can i do that? so converting into integers i mean, or comparing as dimensions, how exactly works that? – Bilal Yilmaz Apr 18 at 8:07
  • 1
    Well I would use expl3, it has all the needed commands for this (including all sensible tests which (unlike \ifthenelse) are even expandable). But if you want to stick to fp you should probably use its tests, (\FPifgt etc check the documentation). – Ulrike Fischer Apr 18 at 8:22
  • thanks this worked now :) \FPifgt \one \two case 1 \else case 2 \fi – Bilal Yilmaz Apr 18 at 8:34
1

\ithenelse compares integer numbers and so is not suited for such tests. Use the tests provided by the fp package itself:

\documentclass[10pt,german]{article}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgffor} %foreach
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp   -> FPeval


 \expandafter\xdef\csname test1\endcsname{5}
 \expandafter\xdef\csname test2\endcsname{4}

\begin{document}


 %\FPeval{\one}{\csname test1\endcsname} <- this works
 \FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname} % <- this doesnt work
 \FPeval{\two}{\csname test2\endcsname}

  \FPifgt \one \two case 1 \else case 2 \fi

\end{document}
0

As soon as you apply \FPeval, you get something like 9.000000000000000000, which is not an integer to TeX's eyes, so the comparison is invalid.

You could use \lengthtest:

\documentclass{article}

\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp   -> FPeval
\usepackage{ifthen}

\expandafter\def\csname test1\endcsname{5}
\expandafter\def\csname test2\endcsname{4}

\begin{document}

\FPeval{\one}{\csname test1\endcsname+\csname test2\endcsname}
\FPeval{\two}{\csname test2\endcsname}

\ifthenelse{\lengthtest{\one pt > \two pt}}{case 1}{case 2}

\end{document}

but in my opinion it is much better to use expl3, that features expandable comparisons.

\documentclass{article}
\usepackage{xparse}

% see https://tex.stackexchange.com/a/467527/4427
\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

% some syntactic sugar for variables
\NewDocumentCommand{\setvar}{mm}
 {% #1 = name, #2 = value
  \fp_zero_new:c { l_bilal_var_#1_fp }
  \fp_set:cn { l_bilal_var_#1_fp } { #2 }
 }
\NewExpandableDocumentCommand{\usevar}{m}
 {
  \fp_use:c { l_bilal_var_#1_fp }
 }
\ExplSyntaxOff

\begin{document}

\setvar{test1}{5} 
\setvar{test2}{4} 
\setvar{one}{\usevar{test1}+\usevar{test2}}

\xifthenelse{\fptest{\usevar{one} > \usevar{test2}}}{case 1}{case 2}


\end{document}
  • He doesn't want to use expl3, said it is weird ;-(. – Ulrike Fischer Apr 18 at 8:59

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.