gtg922r / obsidian-numerals

An obsidian plugin which turns a math code block into a full featured calculator
Other
389 stars 7 forks source link

Add additional units #47

Open lhebates opened 1 year ago

lhebates commented 1 year ago

Hi,

I enjoyed this wonderful plugin since its installation. I started experimenting using it to portion recipe, but ran into issues parsing units such as tsp (teaspoon) or tbsp (tablespoon). Is there a way to add more units to the plug-in or even the potential to add custom units, such as through the settings or a .js file, etc. Thanks!

merchantscp2 commented 1 year ago

Checkout the mathjs documentation. it allows you to create your own custom units.

Also, teaspoon and tablespoon are already implemented. see: https://mathjs.org/docs/datatypes/units.html

freqyfreqy commented 1 year ago

Very cool. I'm not such a pro on this. Would you be able to provide an example of how to create a custom unit with numerals calling this mathjs function? What would be awesome.

merchantscp2 commented 1 year ago

In a Numerals's block, follow the syntax and type something like

createUnit('furlong', '220 yards') Note that you don't need the math. Prefix.

Let the equation execute and when you're done, go to edit it and go out of it. It should give you an error saying you can't have the same unit defined. It should persist after that

freqyfreqy commented 1 year ago

Nice, I did that I see the error you mentioned. How long does this new custom unit persist for? It seems to persist across notes but when I close obsidian and reboot it, the custom unit doesn't seem to be recognized anymore.

gtg922r commented 1 year ago

Definitely plan to support custom units.

The error you are seeing is mathjs throws an error when a unit is created that matches the name of an existing one. Which, currently, will happen if you use createUnit since it is run every time the codeblock is rendered.

I just need to check if the unit already exists and then not re-call the mathjs function. I might need a custom syntax.

merchantscp2 commented 1 year ago

It’d be a cool option to have in the settings that you can specify a unit and a relation to other units. Then when the block runs, have that check to see if the unit exists or refresh it if it changes

On Jul 3, 2023, at 3:25 PM, Ryan C @.***> wrote:

 Definitely plan to support custom units.

The error you are seeing is mathjs throws and error when a unit is created that matches the name of an existing one. Which, currently, will happen if you use createUnit since it is run every time the codeblock is rendered.

I just need to check if the unit already exists and then not re-call the mathjs function. I might need a custom syntax.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

gtg922r commented 1 year ago
emiraga commented 1 year ago

As a temporary hack, this works, and you just need to define unit once on top of the page, and no need to repeat the definition.

```math
createUnit('kcal', '4184 joule', {override: true});
1kcal