Mathjs: how to disable units?

Created on 22 Aug 2018  路  4Comments  路  Source: josdejong/mathjs

Hi, I am quite new to math.js. Hope someone can help me:
(If this is the wrong place to ask, could you please point me to the correct place?)

I would like expressions like v=s/t to be converted to TeX as v = \frac{s}{t} using math.parse("v=s/t");
However, the result is v = \frac{\mathrm{s}}{t}, obviously because s is recognized as a unit and therefor should not be printed in italics.

My question: Is there a way to avoid this (usually correct, but in my case undesired) behaviour?
The easiest way would be a (temporary) deletion of units. There is an internal Unit.deleteUnit function, but I think this is not "public"?
Another option might be to use a customized node.toTex({handler: ...}) call. Can anyone confirm if this is the only/best solution (and if so, maybe even give some hints how to implement the handler) before I spend time for nothing?
Or should I just use a node.toTex().replace(/\mathrm{s}/,"s") afterwards? Would be quite simple, but somehow unsatisfactory.

Thanks in advance!

question

All 4 comments

If you want to disable all units, I think your best bet is custom bundling MathJS. Custom Bundling Documentation.

For example, for a project I'm working on, I use (needs a build tool like webpack):

import core from 'mathjs/core'
const math = core.create()

// First load objects and functions you want
math.import(require('mathjs/lib/type/matrix'))
math.import(require('mathjs/lib/type/complex'))
math.import(require('mathjs/lib/function/arithmetic'))
math.import(require('mathjs/lib/function/trigonometry'))
math.import(require('mathjs/lib/function/matrix'))

// Then load the expression parser
math.import(require('mathjs/lib/expression'))

// Now export (or use) your mathjs instance
export default math

@nioate this is indeed the right place to ask :)

If it's just about printing to tex, your ideas for using a custom toTex handler is the best I think, it should be quite easy to replace the behavior of SymbolNode. A Regexp is also possible but it's typically not very suitable for expressions since it cannot handle precedence of operators and stuff like that.

The idea of @ChristopherChudzicki is interesting too, but I'm not sure if that solves your issue with printing a SymbolNode to Tex.

Is there a way to disable just * some units?

Yes. This is a hack, but I think you can do delete math.Unit.UNITS.kg or whichever unit you'd like to disable. I haven't tried this, though.

Edit: kg is a combination of the prefix k and the unit g, so using this hack you would have to actually do delete math.Unit.UNITS.g.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JasonShin picture JasonShin  路  3Comments

lwirtz picture lwirtz  路  3Comments

skgadi picture skgadi  路  3Comments

zzzgit picture zzzgit  路  4Comments

smith120bh picture smith120bh  路  4Comments