HACKER Q&A
📣 xeonmc

PST-plot for the web? (LaTeX's declarative SVG plot generation)


There are a lot of web components for rendering LaTeX equations.

But an almost equally frequent usecase is plotting functions declaratively using postscript, they are rendered as vector figures and has the signature "elegant black-line plots" style that you associate with math textbooks figures -- in fact the majority of them are probably generated this way, since the authors were already writing in LaTeX to begin with.

An example can be seen here, you'll notice that the figures are all vector based: http://coin.wne.uw.edu.pl/wincenciak/latex/pstricks_plotting.pdf

I see a lot of web components that uses canvas plotting, and none which renders a simple function-plot into a vector graphics figure.

For my particular use case, I'm writing some math guides using MdBook, and while the MathJax support is nice, for simple illustrative plots embedding an entire Desmos canvas is too much, when all that is needed is a static figure.

It would also be desirable to get that "black line math textbook plot" style without too much configuration -- all the web plotting frameworks use the ugly thin-blue-line MS Excel style by default -- but with support for dark theme like MathJax/KaTex does.


  👤 lahvak Accepted Answer ✓
Do you want the TeX code to be converted to svg in the browser at rendering time or are you ok with converting the figures to svg beforehand and including them in your html files?

For the former I would look at TikZjaX (https://github.com/kisonecat/tikzjax) which takes tikz code and convert it to svg on the fly, just like MathJaX converting TeX equations. It may be slow, though, especially for complicated plots.

If you are OK with creating the figures beforehand and serving the generated svg, there are several options: Asymptote (https://asymptote.sourceforge.io/) can generate svg and even html files that you can embed in an iframe. You can convert any TeX figure to svg with dvisvgm (play with the --font-format option). If you insists on using pstricks, I am pretty sure you can convert a resulting postscript file to svg as well. If you are using metapost, then mptopdf will convert metapost source to pdf, which you can then convert to svg.