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.

8
  • Perhaps download.cnet.com/Free-PDF-to-SVG-Converter/… . Google may find other downloadable tools. Jul 15, 2015 at 13:41
  • 2
    inkscape --without-gui --file=input.pdf --export-plain-svg=output.svg (stackoverflow.com/a/10290006/2442087)
    – giordano
    Jul 15, 2015 at 13:43
  • 2
    Yeah, I know these solutions. I'm currently using pdf2svg-0.2.2. But I was hoping for a direct tex-to-svg solution... Jul 15, 2015 at 14:33
  • 2
    ok, i'll elaborate a little: i compile the latex code from inside a servlet. the output is sent to the user as a svg figure in a web page. using tex=>pdf=>svg works in principle, but is more costly in terms of server resources than i assume a single step would be. also, sometimes pdflatex doesn't terminate. i'm looking into the reason for this, but was hoping to avoid the debugging by simply avoiding pdf. Jul 15, 2015 at 16:31
  • 2
    Does batchmode help to avoid the termination problem? See this question
    – Jost
    Jul 15, 2015 at 17:05

4 Answers 4

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
  • 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, 2018 at 14:40
  • this did not work for me: npm WARN EBADENGINE Unsupported engine {
    – con
    Feb 9 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.

6
  • 3
    Could you explain, why going via DVI is an improvement over going via PDF?
    – Jost
    Jul 15, 2015 at 16:16
  • 2
    @Jost Frankly, no, but the OP asked for a pdf-free way to achieve his goal. Jul 15, 2015 at 16:17
  • Not me but the OP asked the question. I'm just curious.
    – Jost
    Jul 15, 2015 at 16:29
  • 2
    @lukas.coenig OP is our slang for "original poster" Jul 15, 2015 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, 2018 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.

0
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.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .