Building dvisvgm and kpathsea on macOS

14 Apr '17 — last updated 27 Jan '18

Update (Jan 2018): TexLive 2017 now has dvisvgm version 2.1.3… which is now almost a year out of date -.-. If you’re interested in how to produce SVG files from TikZ/LaTeX, I got you covered.

dvisvgm converts DVI files produced by LaTeX to SVG. It’s included in TeXLive/MacTeX, which is good enough for most users. But the binary in my (fully upgraded) TeXLive 2016 is somewhat out of date: 1.15.1, which is from 19 April 2016. In the meantime, 2.1.3 has been released.

Luckily, dvisvgm is a very modern and well-maintained project. And homebrew has almost all dependencies we need (automake, freetype, ghostscript, and potrace), with one major exception: kpathsea.

TL;DR: Here’s a script that downloads and builds dvisvgm on macOS. Tested on macOS Sierra 10.12.4.

Kpathsea “is a library to do path searching”, which basically means it’s like which or locate for TeX. Also, “[k]pathsea is maintained as part of the TeX Live distribution. It has never been distributed as a standalone library, though some distros have deployed it that way.” Turns out those distros probably did that because (AFAIK), TeXLive ships with all the binaries statically linked, i.e. without any libraries in the form of shared objects or dynamic libraries. So if you want to rebuild something that depends on kpathsea, you’d basically have to build the TeX distribution yourself. Which is so very tedious, but TeXLive is open source

In fact, it turns out you can just build kpathsea by itself, as it has no other dependencies. But it isn’t that easy, because nothing in TeX can be easy.

Kpathsea needs something to base it’s search paths on. And while the documentation of path sources sounds reasonable, a major pitfall is that most paths are based on a variable called SELFAUTOLOC; this is the (runtime) path of the kpathsea binaries. At least that’s the case when I build it. For MacTeX, not so much:

$ which kpsewhich
$ kpsewhich -var SELFAUTOLOC

So far, I have not been able to figure out how MacTeX manages to hardcode SELFAUTOLOC. I messed around with it for hours. My final solution is to simply patch the function that returns the executable directory (kpathsea_selfdir) to return a hardcoded path to the TeXLive installation directory. Then you can build kpathsea with no issues, and it will point to all the right locations. And with that built, dvisvgm is also easy to build.

Finally, something else to be aware of. Both kpathsea and dvisvgm both use the default prefix /usr/local/, which means e.g. the binaries get installed to /usr/local/bin. As mentioned earlier, MacTeX uses /usr/local/texlive/2016 as a prefix. So when you build kpathsea and dvisvgm, they shouldn’t overwrite anything. But uninstalling them from /usr/local/ is tricky, and it depends on your $PATH order which binaries will get used. To avoid this hassle, I’d recommend using a custom prefix. Then you can just delete the whole prefix folder when you want to uninstall dvisvgm.

To avoid even more hassle, here’s the script I wrote to do it all:

macOS, TeX

Newer Older