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 totrue, the function will prefer using shorthands in Typst (e.g.,->instead ofarrow.r,<<instead oflt.double) when converting TeX to Typst. Default isture.
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 totrue, the Typst result will use the slash notation for fractions. Default istrue.
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 totrue,\inftyconverts toooinstead ofinfinity. Default isfalse.
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 istrue.It tells the converter it's whether in block or inline math environment.
truefor block math environment,falsefor inline math environment. The difference of translation comes when the Typst math code containsdisplayorinlinefunction.
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