parseToLatex

Converts maths typed in plain shorthand, such as x^2/4 or sqrt(2), into LaTeX that KaTeX can render.

@internote/canvas-sdk/reactFunction
On this page
import { parseToLatex } from '@internote/canvas-sdk/react';

A reader types what they would write by hand and sees it set as maths. No parser, just ordered substitution, which is what makes it forgiving enough to run on every keystroke of a half-finished expression. The shorthand it reads:

  • Greek letters by name or as the Unicode letter: pi and π to \pi.
  • inf or infinity to \infty, and +- to \pm.
  • Roots: sqrt(x) to \sqrt{x}, 3rt(x) to \sqrt[3]{x}.
  • Fractions: 2/3 to \frac{2}{3}, x/(a+b) to \frac{x}{a+b}.
  • Powers and subscripts: x^2 to x^{2}, x_(n+1) to x_{n+1}.
  • Functions set upright: sin(x) to \sin(x), ln(x) to \ln(x).
  • * to \times.

Powers are braced before fractions are read, because powers bind tighter than division: x^2/4 is (x^2)/4.

#Signature

(input: string): string

#Parameters

input
The shorthand as typed.

#Example

<Maths>{parseToLatex(typed)}</Maths>

Declared in packages/canvas-sdk/src/react/math/parseToLatex.ts:205.

Last updated