Calculate using Katex

This doesn’t need to be an issue, I’m just not sure how to utilize Katex to actually perform simple calculations. Is this possible with Katex or am I misunderstanding how this works?

Thanks,
Alex

Hi Alex,

Thank you for the question.
You can write equations using math plugin but it does not support ‘calculating’.
I guess KaTeX even does not have such feature.
It’s basically for writing equations in LaTeX syntax.
You can create an issue on their GitHub repository if you’d suggest it.

Could I request some sort of functionality within Inkdrop? If it’s not trivially easy, forget about it, but it would be great if I could do simple equations within tables. I’m using this for my DnD notes.

Actually, I think it would be a super fun way to learn Node… Do you have any guidance for how I could go about making this myself?

I’m thinking I’d want some sort of inline math ability, so I’d look for characters similarly to how KaTeX searches for $1+1$ to display, right? So I’d be thinking of something like ^1+1^

I’ve got a scaffolded repo set up: https://github.com/arkag/inkdrop-calc

I don’t know how you are going to build a calculator based on KaTeX format, but I guess it’d be hard.
You can get started by forking the math plugin though.

Instead of trying to build such a calculator yourself, it’d be a lot easier to just use math.js’ evaluate. Although math.js doesn’t have a KaTeX parser, simple expressions like “1 + 1” in KaTeX is also parseable by math.js. See https://mathjs.org/docs/expressions/syntax.html for math.js’ full syntax.

Huh, how would I go about using that? I was already thinking of leveraging a library for that, but are you saying I wouldn’t need a plugin at all?

You would still need a plugin to use the library with Inkdrop. I think the easiest way would be to create an Inkdrop command which checks if text is selected in the editor, and if so, evaluate it using math.js and replace the selected text with the result.

This code is not tested, but I imagine something like this in a command may work:

// Retrieve the CodeMirror instance of the editor
const { cm } = inkdrop.getActiveEditor();

// Get the selected text
const selected = cm.getSelection();

if (selected !== '') {
  // Evaluate selected text using math.js
  // TODO: Import math.js
  // TODO: Catch any errors, it is not guaranteed that the selected text is a valid math.js expression
  const evaluated = mathjs.evaluate(selected);

  // Replace selection with math.js result
  cm.replaceSelection(evaluated);
}
2 Likes

@Alex made a plugin “calc” here:

https://my.inkdrop.app/plugins/calc

Thanks a lot for the contribution, @Alex and @jmerle !
Closing it.