obsidian-numerals icon indicating copy to clipboard operation
obsidian-numerals copied to clipboard

Add additional units

Open lhebates opened this issue 1 year ago • 8 comments

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!

lhebates avatar May 11 '23 02:05 lhebates

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

merchantscp2 avatar Jun 13 '23 01:06 merchantscp2

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.

freqyfreqy avatar Jun 13 '23 19:06 freqyfreqy

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

merchantscp2 avatar Jun 14 '23 16:06 merchantscp2

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.

freqyfreqy avatar Jun 14 '23 17:06 freqyfreqy

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.

gtg922r avatar Jul 03 '23 19:07 gtg922r

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.

merchantscp2 avatar Jul 03 '23 21:07 merchantscp2

  • @merchantscp2 Good point. Feature should support
    • Custom unit definition in a code-block
    • Custom unit definition in settings for all code-blocks
    • (Lower priority) Unit definition in a code-block persists for all code-block on the page
    • (Lower priority) Unit definition in metadata, e.g. frontmater, that persists across the page

gtg922r avatar Jul 03 '23 21:07 gtg922r

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
```

emiraga avatar Jul 23 '23 16:07 emiraga