Node-TikZJax
A port of TikZJax runs on pure Node.js and WebAssembly.
Node-TikZJax lets you render LaTeX and TikZ diagrams to SVG images without the need to install LaTeX toolchain to the environment. You can render graphs, figures, circuits, chemical diagrams, commutative diagrams, and more.
Installation
npm install node-tikzjax
Examples
See the demo folder for some example TikZ diagrams.
Live demo: https://stackblitz.com/edit/node-tikzjax
![screenshot](https://github.com/prinsss/node-tikzjax/raw/main/demo/screenshot.png)
The example TeX source code is taken from obsidian-tikzjax.
Usage
Basic usage:
import tex2svg from 'node-tikzjax';
const source = `\\begin{document}
\\begin{tikzpicture}
\\draw (0,0) circle (1in);
\\end{tikzpicture}
\\end{document}`;
const svg = await tex2svg(source);
Which generates the following SVG:
<svg xmlns="http://www.w3.org/2000/svg" width="193.253" height="193.253" viewBox="-72 -72 144.94 144.94">
<path fill="none" stroke="#000" stroke-miterlimit="10" stroke-width=".4" d="M72.47.2c0-39.914-32.356-72.27-72.27-72.27S-72.07-39.714-72.07.2-39.714 72.47.2 72.47 72.47 40.114 72.47.2ZM.2.2"/>
</svg>
The input TeX source is rendered to DVI, then rendered to SVG.
- Remember to load any packages you need with
\usepackage{}
, and include \begin{document}
and \end{document}
. - The standalone document class is used (
\documentclass{standalone}
).
The following packages are available in \usepackage{}
:
- chemfig
- tikz-cd
- circuitikz
- pgfplots
- array
- amsmath
- amstext
- amsfonts
- amssymb
- tikz-3dplot
[!NOTE]
Don't run multiple instances of node-tikzjax
at the same time. This may cause unexpected results.
Advanced Usage
There are some options you can pass to tex2svg()
:
const svg = await tex2svg(source, {
showConsole: true,
texPackages: { pgfplots: '', amsmath: 'intlimits' },
tikzLibraries: 'arrows.meta,calc',
addToPreamble: '% comment',
embedFontCss: true,
fontCssUrl: 'https://cdn.jsdelivr.net/npm/node-tikzjax@latest/css/fonts.css',
disableOptimize: false,
});
You can also separate the TeX rendering and DVI to SVG conversion steps:
import { load, tex, dvi2svg } from 'node-tikzjax';
await load();
const input = readFileSync('sample.tex', 'utf8');
const dvi = await tex(input, {
showConsole: true,
texPackages: { pgfplots: '', amsmath: 'intlimits' },
tikzLibraries: 'arrows.meta,calc',
addToPreamble: '% comment',
});
writeFileSync('sample.dvi', dvi);
const svg = await dvi2svg(dvi, {
embedFontCss: true,
fontCssUrl: 'https://cdn.jsdelivr.net/npm/node-tikzjax@latest/css/fonts.css',
disableOptimize: false,
});
writeFileSync('sample.svg', svg);
Acknowledgements
This project is greatly inspired by obsidian-tikzjax.
This port would not be possible without the original tikzjax project.
Prebuilt WebAssembly binaries and supporting files are downloaded from artisticat1/tikzjax#output-single-file.
A fork of artisticat1/dvi2html#ww-modifications is used to convert DVI to SVG.
The BaKoMa fonts shipped with this package are licensed under the BaKoMa Fonts Licence.
Thanks to all the authors for their great work!
License
LaTeX Project Public License v1.3c