Error when using command as parameter for an other command

Error when using command as parameter for an other command



I want to use 2 seperate commands to first execute a calculation on a number and then displaying the number with plus or minus using following code.



When trying to run it I get various errors. I think that propably FPeval is causing problems.


FPeval


documentclassscrbook
usepackagefp
usepackageifthen

newcommandMyHalf[1]FPevalresulttrunc(#1 / 2, 0)result

newcommandMyDisplaySign[1]ifthenelse#1<0$#1$$+#1$

begindocument

MyHalf12

MyDisplaySignMyHalf12

enddocument






FPeval is not expandable, it assigns the result to result, so you need to do the evaluation first and then pass result to the expansion context in the numeric test.

– David Carlisle
Sep 8 '18 at 18:38


FPeval


result


result




2 Answers
2



The problem is that for the ifthenelse test compare #1<0, #1 has to be a number. But if you pass a macro that contains a number (or, in some way, expands to a number), that macro has to be expandable, so that ifthenelse can performing the test. The problem, as David said in the comment, is that FPeval is not expandable because it assigns the result to result.


ifthenelse


#1<0


#1


ifthenelse


FPeval


result



But fp_eval:n is expandable :)


fp_eval:n


documentclassscrbook

usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandMyHalf
m

fp_eval:n trunc ( #1 / 2, 0 )

NewExpandableDocumentCommandMyDisplaySign
m

int_compare:nTF
#1 <= c_zero_int
$ #1 $
$ + #1 $

ExplSyntaxOff

begindocument

MyHalf12 % Prints 6

MyDisplaySignMyHalf12 % Prints +6

MyDisplaySignMyHalf-12 % Prints -6

MyDisplaySignMyHalf0 % Doesn't print "+0" because 0 isn't larger than c_zero_int, so the conditional evaluates to true

MyDisplaySignMyHalf-0 % Prints -0 because of the leading sign

enddocument



The only issue is with 0. With the code above you don't get the leading + sign because 0=0 is true, so the first (for negative numbers) is taken. If you want the leading + sign then you have to print the sign if the value is equal to zero too, but then with negative zero you get +-0, because -0=+0 and the sign is added too:


0


+


0=0


+


+-0


-0=+0


documentclassscrbook

usepackagexparse
ExplSyntaxOn
NewExpandableDocumentCommandMyHalf
m

fp_eval:n trunc ( #1 / 2, 0 )

NewExpandableDocumentCommandMyDisplaySign
m

int_compare:nTF
#1 >= c_zero_int
$ + #1 $
$ #1 $

ExplSyntaxOff

begindocument

MyHalf12 % Prints 6

MyDisplaySignMyHalf12 % Prints +6

MyDisplaySignMyHalf-12 % Prints -6

MyDisplaySignMyHalf0 % Prints +0

MyDisplaySignMyHalf-0 % Prints +-0

enddocument



The isn't, as far as I know, a way to get -0 and +0 correct without doing extra tests for this specific scenario.


-0


+0






ifthenelse does expand the argument in the test, the problem is rather that FPeval is not expandable. (but using fp_eval is better, I agree:-)

– David Carlisle
Sep 8 '18 at 18:40



ifthenelse


FPeval






@DavidCarlisle Ooh. Thanks, I'll fix it :)

– Phelype Oleinik
Sep 8 '18 at 18:41






This would print +0 With int_compare:nTF #1 <= 0 you'd solve the issue. Or reverse the test: int_compare:nNnTF #1 > 0 $+#1$ $#1$

– egreg
Sep 8 '18 at 21:25



+0


int_compare:nTF #1 <= 0


int_compare:nNnTF #1 > 0 $+#1$ $#1$






@egreg Well pointed. Thanks :)

– Phelype Oleinik
Sep 8 '18 at 21:49






The changed version still prints +0.

– egreg
Sep 9 '18 at 16:34


documentclassscrbook
usepackagefp
usepackageifthen

newcommandMyHalf[1]FPevalresulttrunc(#1 / 2, 0)

newcommandMyDisplaySign[1]ifthenelse#1<0$#1$$+#1$

begindocument

MyHalf12%
% Now the result is stored in the macro result.
% Let's see the result:
result

% Now let's see the result with preceding algebraic sign:
MyDisplaySignresult

enddocument



Thanks for contributing an answer to TeX - LaTeX Stack Exchange!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?