28

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.

4 Answers4

12

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
  • 2
    You may need to use sudo npm install --global mathjax-node-cli and /usr/local/lib/node_modules/mathjax-node-cli/bin/tex2svg '\sin^2{\theta} + \cos^2{\theta} = 1' > test.svg now – omnomnom Feb 14 '18 at 14:40
  • this did not work for me: npm WARN EBADENGINE Unsupported engine { – con Feb 09 '24 at 16:44
9

You can use classical LaTeX (with dvi output) and dvisvgm ( http://www.ctan.org/pkg/dvisvgm ) to convert the dvi file to SVG.

EDIT: I am not aware of a *svgtex implementation that does the conversion in one step.

  • 3
    Could you explain, why going via DVI is an improvement over going via PDF? – Jost Jul 15 '15 at 16:16
  • 2
    @Jost Frankly, no, but the OP asked for a pdf-free way to achieve his goal. – Sir Cornflakes Jul 15 '15 at 16:17
  • Not me but the OP asked the question. I'm just curious. – Jost Jul 15 '15 at 16:29
  • i (the "OP", i assume - what does it mean anyway?) elaborated above why i looked for a pdf-free method. i'll certainly try your suggestion using dvi - thanks for that! However, i'm starting to feel that pdflatex is not the real problem, and i'll run into the same trouble with dvi... – lukas.coenig Jul 15 '15 at 16:34
  • 2
    @lukas.coenig OP is our slang for "original poster" – Sir Cornflakes Jul 15 '15 at 16:36
  • 1
    Also possible: xelatex --no-pdf producing extended DVI (file ending xdv) and subsequent conversion to SVG with dvisvgm. – AlexG Nov 26 '18 at 10:09
3

In my windows, this works,

Install:

  1. Ghostscript (select 64 bit)
  2. Inkscape (select 64 bit)

Then add the "bin" folder to path.

For example, I add "C:\Program Files\Inkscape\bin" and "C:\Program Files\gs\gs9.54.0\bin" to the user path.

If you have a "document.tex" file, then

pdflatex document.tex
gswin64 -dNoOutputFonts -sDEVICE=pdfwrite -o tmp.pdf document.pdf
inkscape -o document.svg tmp.pdf

Here gswin64 can convert the text to path, avoid the font error when inkscape convert to svg. Although the inkscape has "-T" option that convert text to path, however, this doesn't work sometimes expectially when you are use tikzpicture.

1

Here's a command line tool to convert mathematical expressions in tex to svg (via pdflatex and pdf2svg): https://github.com/yannikschaelte/tex2svg

Example usage:

tex2svg '$\exp(i\cdot\pi) + 1 = 0$' out.svg

(Disclaimer: I'm the author, so likely biased.)