Action widget to set fields with math operations

84 views
Skip to first unread message

Guille Rmo

unread,
Aug 22, 2018, 2:14:47 PM8/22/18
to TiddlyWiki
I found math.js plugin for tw a while ago. However I didn't managed to do what I wanted with it. I wanted to set a field value with a math expression. I remember I managed to do it with a macro and a $set variable, but I think that is not very clean. Instead, I thought that making an alternative action-setfield widget was the right thing to do. So I took @mklauber plugin and wrote the widget.

See the following examples to get an Idea of what is possible with this:

This button raises the field "test2" to the power of 2 each time it is clicked:

<$button>
<$action-setfield-mathjs $tiddler=<<currentTiddler>> $field="test2" $eval="test2^2" />
Click to raise <b>{{!!test2}}</b> to the power of 2
</$button>

You can also manipulate data tiddlers. In the following, the data tiddler [[ExampleData]] is updated with math operations:

<$button>
<$action-setfield-mathjs $tiddler="ExampleData" $index="y" $eval="y+1" />
Click to increment y ( {{ExampleData##y}} )
</$button>

<$button>
<$action-setfield-mathjs $tiddler="ExampleData" $index="x" $eval="x+1" />
Click to increment x ( {{ExampleData##x}} )
</$button>

The tiddler fields or the data tiddler indexes are set as the math.js scope, so you can use the names in the math.js expressions.

The main motivation for this was having a simple increment button :) But it is quite powerful. Hope you can come up with great use cases for this.



TonyM

unread,
Aug 23, 2018, 12:52:41 AM8/23/18
to TiddlyWiki
Guille Rmo

Thanks for sharing your work. Increments have being difficult in TiddlyWiki, this works good as a button counter.

You may be interested in the new range operator coming in V 5.1.18

Regards
Tony

Guille Rmo

unread,
Aug 23, 2018, 12:35:38 PM8/23/18
to TiddlyWiki
Thanks Tony. 

I have googled a bit and found these alternatives to this same need:


These are quite awesome already. I really liked SocialCalc, and mathcell uses native eval which is not great.

I think math.js is great and is good enough to be taken as a standard for TW, for evaluating math expressions, however SocialCalc solution is great as it is.

Diego Mesa

unread,
Aug 23, 2018, 1:00:15 PM8/23/18
to TiddlyWiki
Formulas!!! I think it should be part of the core and can highly recommend it.
Reply all
Reply to author
Forward
0 new messages