Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to check whether a number \a (read from a file) is smaller than zero using ifthenelse:

\ifthenelse{\a<5}{smaller}{not smaller}

This works fine when a is in the format -1. In the file commas (,) are used as decimal separators, so \ifthenelse fails.

Any ideas on how to compare such a number of format -1,23?

PS: I know siunitx could probably do it, but it seems to conflict with csvsimple, so it is not an option.

share|improve this question
6  
The standard test of \ifthenelse compares only integer numbers. You may try \ifthenelse{\lengthtest{\a pt<0pt}}{true}{false} which should be indifferent to the decimal separator (comma or period). –  egreg Nov 28 '12 at 11:55
    
You are right. \lengthtest seems to only work with , for testing if \a is larger/smaller than zero (not other values). But this is already very good. –  Tim Nov 28 '12 at 12:16
    
I don't fully understand this comment. You can also compare with other values, for instance \lengthtest{\a pt<5pt}. –  Stephan Lehmke Nov 28 '12 at 12:46
1  
You can also use \ifdim \a pt<2,5pt x \else y \fi (note the pt after the numbers to be tested). –  Jake Nov 28 '12 at 12:48
    
The problem is the comma. English (and TeX as well) use dot as a seperator. We would need to know more, but the first answer here (link) can be easily reverted to replace , by .. –  tohecz Nov 28 '12 at 12:50
show 1 more comment

1 Answer 1

up vote 8 down vote accepted

A decimal number can always be input either with the comma or the period, when TeX's length tests are concerned.

On the other hand, the standard test for \ifthenelse compares integer numbers. So we need to do differently:

\ifthenelse{\lengthtest{\a pt < 0pt}}{smaller}{not smaller}

will do, because \a will expand to the decimal number and adding the unit point to it will give a legitimate length to be compared with 0pt.

One is not forced to use 0pt: any length is good. So if the test must compare if the given number is less than 1.23,

\ifthenelse{\lengthtest{\a pt < 1.23pt}}{smaller}{not smaller}

will do. This, of course, will compare correctly numbers with a short decimal part (TeX compares correctly up to the fourth decimal digit, the fifth may not be recognized because of the internal binary arithmetic with truncation).

Either commas or periods for delimiting the decimal part can be used safely.

share|improve this answer
    
As usual, a very useful answer for more than one LaTeX-user! Thank you, egreg! –  Sveinung Nov 28 '12 at 20:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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