Control SVG Groups in LaTeX

2020-06-17

I love the powerful LaTeX drawing package TikZ and use it whenever I can, in particular for creating SVGs. Converting from PDF to SVG with Inkscape usually gives good results, visually — but what if the structure of the XML is important, as well?

For example, you may want to manually tweak the arrangement of certain elements in Inkscape. For this, it is important that SVG groups are meaningful or at least not harmful. Unfortunately, as I describe on tex.SE, the lualatex+Inkscape pipeline can create pathological group arrangements that are a pain to work with.

Helpful user AlexG reminded me of the dvilualatex+dvisvgm pipeline. Given that dvisvgm has specifically been developed to consume LaTeX-generated files, it is certainly worth a shot!

And indeed, we can instrument the DVI by inserting special LaTeX commands in the source:

\special{dvisvgm:raw <g id="My Group">}
% any code
\special{dvisvgm:raw </g>}

The intermediate DVI looks just the same, but now dvisvgm knows to inject the given SVG snippets into its output.

TipSome tweaking of the dvisvgm call may be necessary depending on your document. For instance, I needed to add --no-fonts to force it to convert text into SVG paths instead of trying to include the font, which did not work in my setup.
LaTeXTool
Comment & Share on Twitter 

GitHub Actions: Create Releases With Changelog and Assets

Logging in Go With Zap