Help in using calc macro

143 views
Skip to first unread message

Mr. Mal

unread,
Mar 11, 2016, 4:42:17 AM3/11/16
to TiddlyWiki
 Hello everyone.

I try to make some calculation using calc macro by TB. But connot figure out how to solve this problem.

see image below. TQ
Screenshot at 2016-03-11 17:39:24.png

Matthew Lauber

unread,
Mar 11, 2016, 7:08:04 AM3/11/16
to TiddlyWiki
What you're running into is the issue with macros. Macros are evaluated at render time, as a strict text replacement.  So <<calc !!order /7 decimals:2>> puts the text of the field order (not the resolved value) in place of !!order.  Therefore, you're code becomes <<calc <<calc !!foo + by:!!bar>> /7 decimals:2>>.  Since TiddlyWiki doesn't support macros embedded in macros, calc is trying to process the string "<<calc !!foo + by:!!bar>>" divided by 7.  And of course, you can't divide that string by 7, so it says the answer is NaN.  

Unfortunately, there's not a great answer to how to fix this.  I would look into using Tobias' eval widget in place of his calc macro.  widgets do support embedding one widget inside another, so you would be able to transclude one expression inside another and it would resolve the result of the transcluded expression before evaluating the outer expression.  Beyond that, perhaps Tobias can chime in with any suggestions he may have.  

Matt

Mr. Mal

unread,
Mar 11, 2016, 8:14:22 AM3/11/16
to TiddlyWiki
Hi Matt. Thank you for explain such detail for this problem.

Matthew Lauber

unread,
Mar 11, 2016, 8:26:07 AM3/11/16
to TiddlyWiki
Glad to Help!  I've been bitten by it myself.  Figure I'll save someone else the 2+ hours it took me to figure out what was going on first time I tried to embed macros in macros.

c pa

unread,
Mar 11, 2016, 6:14:07 PM3/11/16
to TiddlyWiki
So try

<$macrocall $name="calc" value={{!!order}} operation="/"  by="7" decimals:2 />

You can use $macrocall to pass evaluated values to a macro

Mr. Mal

unread,
Mar 11, 2016, 6:51:58 PM3/11/16
to TiddlyWiki
Thanks c pa. Will try tonight.

Mr. Mal

unread,
Mar 11, 2016, 11:06:11 PM3/11/16
to TiddlyWiki
Still get NaN.

Mr. Mal

unread,
Mar 11, 2016, 11:08:54 PM3/11/16
to TiddlyWiki
See attachment.
Screenshot at 2016-03-12 12:06:38.png

c pa

unread,
Mar 14, 2016, 2:17:33 PM3/14/16
to TiddlyWiki
OK I see that the field order contains a macro call so it needs to be evaluated before you pass it to calc. So do the following:

\define precalc(value, operation, by)
    <$macrocall $name="calc" value="$value$ operation="$operation$"  by="$by$" decimals:2 />
\end

<$macrocall $name="precalc" value={{!!order}} operation="/"  by="7"  />

Mr. Mal

unread,
Mar 15, 2016, 12:36:50 AM3/15/16
to TiddlyWiki
Thanks  c pa. You made it. but I have to edit the macro like this

\define precalc(value, operation, by)
<$macrocall $name="calc" value=$value$ operation="$operation$"  by="$by$" decimals:2 />
\end

just delete the inverted comma before $value$. Thanks again.
Reply all
Reply to author
Forward
0 new messages