API Reference of tex2typst.js

November 6, 2025 ยท View on GitHub

Basic usage

import { tex2typst, typst2tex } from 'tex2typst';

let tex = "e \\overset{\\text{def}}{=} \\lim_{{n \\to \\infty}} \left(1 + \\frac{1}{n}\\right)^n";
let typst = tex2typst(tex);
console.log(typst);
// e eq.def lim_(n -> infinity) (1 + 1/n)^n

let tex_recovered = typst2tex(typst);
console.log(tex_recovered);
// e \overset{\text{def}}{=} \lim_{n \rightarrow \infty} \left(1 + \frac{1}{n} \right)^n

Advanced options

Options for tex2typst()

tex2typst function accepts an optional second argument, which is an object containing options to customize the conversion.

interface Tex2TypstOptions {
    preferShorthands: boolean;
    fracToSlash: boolean;
    inftyToOo: boolean;
}
  • preferShorthands: If set to true, the function will prefer using shorthands in Typst (e.g., -> instead of arrow.r, << instead of lt.double) when converting TeX to Typst. Default is ture.
let tex = "a \\rightarrow b \\ll c";
let typst1 = tex2typst(tex, { preferShorthands: false });
console.log(typst1);
// a arrow.r b lt.double c
let typst2 = tex2typst(tex, { preferShorthands: true });
console.log(typst2);
// a -> b << c
  • fracToSlash: If set to true, the Typst result will use the slash notation for fractions. Default is true.
let tex = "\\frac{a}{b}";
let tpyst1 = tex2typst(tex, { fracToSlash: false });
console.log(typst1);
// frac(a, b)
let typst2 = tex2typst(tex, { fracToSlash: true });
console.log(typst2);
// a / b
  • inftyToOo: If set to true, \infty converts to oo instead of infinity. Default is false.
let tex = "\\infty";
let typst1 = tex2typst(tex, { inftyToOo: false });
console.log(typst1);
// infinity
let typst2 = tex2typst(tex, { inftyToOo: true });
console.log(typst2);
// oo

Options for typst2tex()

interface Typst2TexOptions {
    blockMathMode: boolean;
}
  • blockMathMode: Default is true.

    It tells the converter it's whether in block or inline math environment. true for block math environment, false for inline math environment. The difference of translation comes when the Typst math code contains display or inline function.

let typst1 = "a = display(sum_i x_i) b";
console.log(typst2tex(typst1, { blockMathMode: false }));
// a = \displaystyle \sum_i x_i \textstyle b
console.log(typst2tex(typst1, { blockMathMode: true }));
// a = \displaystyle \sum_i x_i b

let typst2 = "a = inline(sum_i x_i) b";
console.log(typst2tex(typst2, { blockMathMode: false }));
// a = \textstyle \sum_i x_i b
console.log(typst2tex(typst2, { blockMathMode: true }));
// a = \textstyle \sum_i x_i \displaystyle b