npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tex2svg-webworker

v0.6.0

Published

Convert LaTeX to SVG via Web Worker

Downloads

69

Readme

tex2svg-webworker

This library enables a browser application to render LaTeX math expressions into SVG with outlined symbols (so no external fonts are required). All rendering is done in the background using a Web Worker, so it will not block the main JavaScript thread.

This library is a simple wrapper around MathJax, similar to their tex2svg Node demo, but built to be used as a Web Worker .js file.

Usage

Worker Creation, Option 1: CDN

  • In your JavaScript code, create a Web Worker and prepare to receive messages:

    worker = new Worker(window.URL.createObjectURL(new Blob([
      "importScripts('https://cdn.jsdelivr.net/npm/tex2svg-webworker/dist/tex2svg.js');"
      // or specify a specific version via .../npm/[email protected]/dist/...
    ], type: 'text/javascript'));
     worker.onmessage = function(e) {
       // e.data.svg is a string of the form "<svg...>...</svg>"
       // All inputs are also available; for example, e.data.formula
     };

Worker Creation, Option 2: NPM

  • npm install tex2svg-webworker

  • Copy node_modules/tex2svg-webworker/dist/tex2svg.js to your web directory.

  • In your JavaScript code, create a Web Worker and prepare to receive messages:

    worker = new Worker('webpath/tex2svg.js');
    worker.onmessage = function(e) {
      // e.data.svg is a string of the form "<svg...>...</svg>"
      // All inputs are also available; for example, e.data.formula
    };

Rendering LaTeX via Worker

  • Send rendering requests to the Web Worker as follows:

    worker.postMessage({
      formula: "e^{\\pi i} + 1 = 0",
      display: true,
    });

Note that the formula input does not include delimiters (e.g. $). Additional options em, ex, and containerWidth are available; see tex2svg.coffee.

Example

See index.html for a simple example of the rendering request/response loop.

Live demo of it in action

Cocreate is a larger application (a shared whiteboard supporting LaTeX text) using this library (and indeed it was the reason I wrote it). The relevant code is in client/RenderObjects.coffee.

Supported LaTeX Commands

In addition to MathJax's standard TeX/LaTeX commands, this library includes several of MathJax's TeX/LaTeX extensions:

  • ams: \begin{align}, etc.
  • amscmd: \begin{CD}
  • boldsymbol: \boldsymbol{bold++}
  • braket: \braket{\phi | \psi}, \set{x \in X | x > 0}, \Set{\sum_{i=1}^n x^i | x \leq 1\}
  • bussproofs: \begin{prooftree}
  • cancel: \cancel{x}/\cancel{x}
  • centernot: \centernot\longrightarrow
  • color: \textcolor{purple}{hi}
  • colortbl: \rowcolor, \columncolor, \cellcolor
  • gensymb: \degree, \celsius, \micro, \ohm, \perthousand
  • mathtools: \coloneq, \eqcolon, ...
  • mhchem: \ce
  • newcommand: \def, \newcommand, \newenvironment (note that these span across multiple expressions)
  • noerrors: render source in case of error
  • noundefined: show undefined commands in red instead of throwing error
  • setoptions: \setOptions{tagSide=left}, etc.
  • textcomp: \textasciitilde, etc.
  • textmacros: \text{$x$ is \emph{good}}
  • upgreek: \upalpha, etc.
  • verb: \verb|$('div')|

There is no need to \require any of these packages to use them.

On the other hand, the following packages are not currently included:

  • action (MathML irrelevant for SVG output)
  • autoload (irrelevant for Web Worker)
  • bbox (use more standard \colorbox{$...$} instead)
  • cases (tags aren't great in SVG output)
  • configmacros (irrelevant for Web Worker)
  • empheq (seems rarely used)
  • enclose (MathML irrelevant for SVG output)
  • extpfeil (not standard LaTeX)
  • html (incompatible with SVG output)
  • require (irrelevant for Web Worker)
  • physics (redefines e.g. \div to mean ∇ (\grad) instead of ÷)
  • tagformat (irrelevant for Web Worker)
  • unicode (doesn't work well with SVG)