ANN: latex-svg packages

Posted on 2020-03-06 by Oleg Grenrus

I occasionally write blog posts with some math. So far I have been using latex-formulae family of packages. This week Andres Löh asked whether \text{\LaTeX} can be rendered as SVGs.

The answer is yes.

The preview CTAN package, and the dvisvgm tool make it very easy. I forked latex-formulae into latex-svg to use above mentioned tools. There are three packages on Hackage now:

To mention some differences between latex-formulae and latex-svg:

  • latex-svg-image uses only latex and dvisvgm, ImageMagick is not required. You don't need to tweak policy.xml to allow rasterization of PostScript. (See this StackOverflow question).

  • latex-svg-image supports global cache (off by default), which speedups hakyll site builds. My blog builds in 20 seconds instead of three minutes with empty cache. I wouldn't worry about space usage too much, the cache contents take 15M after building my blog. which is way less than the size of site executable.

  • latex-svg-hakyll has initFormulaCompilerSVGPure variant, which doesn't need IO to be created. You don't need to thread renderFormulae function through. Together with global cache, the perfomance penalty is small.

  • One drawback is that result pages become bigger (and slower). For example a formula-heavy is

    • 576k in size with latex-formulae
    • 2819k in size with latex-svg (almost 5 times larger).

    Rendering SVG images is also more CPU expensive.

Otherwise the API and module names are the same. If you are already a user of latex-formulae you can easily migrate to latex-svg.

At the end few examples. We can type set quadratic formula


or an exponent tower like


or a charterization of equality of real numbers using "downsets"

(x = y) \leftrightarrow (\forall z. (z \le x) \leftrightarrow (z \le y))

from a recent post by Brent Yorgey.

The difference is clearly visible on high-DPI displays, for example mobile devices.

#Update 0.2: Font size and raw tex blocks

As the font-size can be configured, the inline math like x^2 + y^2 looks nicer. Also note that the baseline is taken into account.

For example you can draw tikz-cd diagrams:

\begin{tikzcd} A \arrow[d, "g"] \arrow[r, "f"] & B \arrow[r, "\alpha"] \arrow[d, "\gamma"] & D \arrow[d, "\beta"] \\ C \arrow[r, "h"] & B&39; \arrow[r, "\lambda"] & D&39; \end{tikzcd}

For this to work the Ext_raw_tex pandoc extension should be enabled.

Site proudly generated by Hakyll