JavaScript

mLaTeX compiled to WebAssembly, for web pages, workers and Node.

The module

mLaTeX compiles to WebAssembly with Emscripten. The build leaves three files side by side:

FileWhat it is
mlatex.jsA factory that loads the module.
mlatex.wasmmLaTeX.
mlatex.dataThe fonts, packed into the module's own file system.

Installing

The mlatex package carries the three files, and its types. It is not published yet; see Installation.

npm install mlatex

A session

import mLatex from "mlatex";

const mlatex = await mLatex();                     // mlatex.js's factory
const session = new mlatex.Session();

session.set("customer", "Acme Corporation");
session.define("balance", 1, (account) => ledger[account].toFixed(2));
session.provide("chart.png", new Uint8Array(await chart.arrayBuffer()));

if (!session.typeset(source)) console.error(session.error());
const pdf = session.pdf();                         // a Uint8Array of its own
session.delete();                                  // as every bound object is

The operations

Every operation is the C++ session's, under the same name: set and unset, define and forget, provide and withdraw, typeset, snippet, pdf and error. See Sessions. Only what JavaScript hands over differs.

Commands

A command is a JavaScript function, called with each argument's text. What it returns is turned into text with String().

Files

A file is a Uint8Array or a string.

The PDF

The PDF comes back as a Uint8Array that owns its bytes, so it outlives the next document.

delete()

Call delete() on a session when you are done with it, as with every object Emscripten binds.

Latex.version

In Node, Latex.version is the installed package's version, such as 0.1.0, read from its own package.json. latex.d.ts declares it.


Building it

The libraries

The libraries mLaTeX uses are built for the wasm32-emscripten triplet:

vcpkg install harfbuzz libpng libjpeg-turbo libwebp zlib --triplet wasm32-emscripten

The wasm preset

Configure with the wasm preset:

cmake --preset wasm

Environment variables

The preset needs the EMSDK and VCPKG_ROOT environment variables set.


Limits

Threads

A document runs on the thread that asked for it, and the module starts no threads of its own, so it needs no shared memory and runs anywhere WebAssembly does.

The network

The module makes no network requests. A picture from a URL is not fetched from inside it: the page fetches it and hands the bytes in with provide, under the name the document uses.

Each release on GitHub carries the module as mlatex-0.1.0-wasm.zip, holding mlatex.js, mlatex.wasm and mlatex.data.