[Tex/LaTex] Compile TeX (directly) into SVG using the command line

svg

Is there a way to directly compile TeX code into the SVG image format (rather than going the detour over TeX => PDF => SVG)?

I found this question:
Convert LaTeX to SVG online

But I don't want to do it online – it should be a command-line call.

Best Answer

tex2svg

The command tex2svg becomes available once mathjax-node-cli in combination with node.js is installed.

With (X)Ubuntu LTS, this requires only two installation steps:

$ sudo apt install nodejs npm
$ sudo npm install --global mathjax-node-cli

The command:

$ /usr/local/lib/node_modules/mathjax-node-cli/bin/tex2svg '\sin^2{\theta} + \cos^2{\theta} = 1' > test.svg

will yield:

tex2svg output, rasterised at 300 dpi

Note 1: If node is used with NVM, the path may be different. The path can be found with:

$ type node
/usr/bin/node

Note 2: The generated SVG file renders properly inside a browser but cannot be displayed using standard image tools.

Note 3: Generating SVG from a TeX file can be done as follows:

$ cat YOURSOURCEFILE.tex | xargs -0 -t -I % /usr/local/lib/node_modules/mathjax-node-cli/bin/tex2svg '%' > YOURENDFILE.svg
Related Question