Fork me on GitHub

addons/mathJson

This module parses and outputs an Abstract Syntax Tree representing the formula using the MathJSON format.

To use it, use the MathAtom.toAST() method.

Methods

asLatex(ast: object): stringprivateinner

ast : object

Abstract Syntax Tree object (in canonical form)

: string   

A LaTeX representation of the AST

asMachineNumber(node: object): numberprivateinner

node : object

Abstract Syntax Tree node

: number   

A JavaScript number, the value of the AST or NaN

asSymbol(expr: object): stringprivateinner

expr : object

Abstract Syntax Tree object

: string   

A string, the symbol, or undefined

escapeText(s: string): stringprivateinner

Return a string escaped as necessary to comply with the JSON format

s : string
: string   

formatMantissa(m: string, config: [string]:any)privateinner

Return a formatted mantissa: 1234567 -> 123 456 7... 1233333 -> 12(3)

m : string
config : [string]:any

getCanonicalName(latex,: string): stringprivateinner

latex, : string

for example '\times'

: string   

the canonical name for the input, for example '*'

getLatexForSymbol(name: string): stringprivateinner

name : string

symbol name, e.g. "alpha"

: string   

getLatexTemplateForFunction(name: string): stringprivateinner

name : string

function canonical name

: string   

getPrecedence(canonicalName,: string): numberprivateinner

Given a canonical name, return its precedence

canonicalName, : string

for example "and"

: number   

isAtom(expr: object, type: string, value: string)privateinner

Return true if the current atom is of the specified type and value.

expr : object
type : string
value : string

negate(node: object)privateinner

Return the negative of the expression. Usually { fn:'negate', arg } but for numbers, the negated number

node : object

numberAsLatex(num: string | number): stringprivateinner

num : string | number

A number, represented as a string (e.g. "-12.45" particularly useful for arbitrary precision numbers) or a number (-12.45)

: string   

A LaTeX representation of the AST

opPrec(atom: object): numberprivateinner

Return the operator precedence of the atom or -1 if not an operator

atom : object
: number   

parse(atoms: Atoms[]): objectprivateinner

atoms : Atoms[]
: object   

parseDelim()privateinner

Delimiters can be expressed:

  • as a matching pair of regular characters: '(a)'
  • a as 'leftright' expression: '\left(a\right)'
  • as a matching pair of 'sizeddelim': '\Bigl(a\Bigr)

Note that the '\delim' command is only used for delimiters in the middle of a \left\right pair and not to represent pair-matched delimiters.

This function handles all three cases

parseDigraph()privateinner

Some symbols are made up of two consecutive characters. Handle them here. Return undefined if not a digraph. TODO: other digraphs: := ++ ** =: °C U+2103 °F U+2109

parseExpression(expr: object): objectprivateinner

Given an atom or an array of atoms, return their AST representation as an object.

expr : object

An expressions, including expr.atoms, expr.index, expr.minPrec the minimum precedence that this parser should parse before returning; expr.lhs (optional); expr.ast, the resulting AST.

: object   

the expr object, updated

parsePostfix()privateinner

Parse postfix operators, such as "!" (factorial)

parseSentence(expr: object): objectprivateinner

Parse a sequence of text zone and math zones: := (() )+

expr : object
: object   

parseSupsub(expr: object)privateinner

Parse for a possible sup/sub attached directly to the current atom or to a following 'msubsup' atom. After the call, the index points to the next atom to process.

expr : object

wrapFence(fence: string): stringprivateinner

fence : string

The fence to wrap around the arguments

: string   
  • A string wrapped in the fence

wrapFn(functionName: string, …params: object)privateinner

functionName : string
params : object repeatable