# Compile TeX (directly) into SVG using the command line

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.

• inkscape --without-gui --file=input.pdf --export-plain-svg=output.svg (stackoverflow.com/a/10290006/2442087) – giordano Jul 15 '15 at 13:43
• Yeah, I know these solutions. I'm currently using pdf2svg-0.2.2. But I was hoping for a direct tex-to-svg solution... – lukas.coenig Jul 15 '15 at 14:33
• 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. – lukas.coenig Jul 15 '15 at 16:31
• Does batchmode help to avoid the termination problem? See this question – Jost Jul 15 '15 at 17:05

# 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: 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

• 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

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.

• Could you explain, why going via DVI is an improvement over going via PDF? – Jost Jul 15 '15 at 16:16
• @Jost Frankly, no, but the OP asked for a pdf-free way to achieve his goal. – jk - Reinstate Monica 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
• @lukas.coenig OP is our slang for "original poster" – jk - Reinstate Monica Jul 15 '15 at 16:36
• 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