Skip to content

Generate SVG output with dvisvgm #21

@DavidPowell

Description

@DavidPowell

In my fork I added a new output method for generating SVGs, using latex and dvisvgm (instead of pdflatex and imagemagick). It has the following advantages and issues:

  • Elements in the SVG output can now have SVG ids (via the --svgid option). This is enables parts of the figure to be manipulated with javascript to animate them etc.

  • Text in the image now appears as selectable text in the output, instead of SVG paths

  • Imagemagick is not needed (one less dependency to deal with, esp on Windows)

  • The output image is sized automatically according to the size of the contents

    • A given element will always have the same size in different images
    • The dimensions specified in TikZ now corresponds to the screen size in cm
    • Unwanted whitespace is avoided (a problem with current SVG backend)
  • Note that to achieve this, I had to change the bounding box size from px (pixels) to pt (physical points)

    • Will this be an unexpected change for existing users? (it could be made specific to the dvisvgm backend)
  • The scale parameter to the tikzpicture{} environment does not scale all objects

    • An --imagescale parameter has been added (for dvisvgm only) to rescale the whole image
    • This parameter might also be useful for other cases
  • Tikz actually has a specific dvisvgm driver, which can be specified with \usepackage[dvisvgm]{standalone}.
    I originally used this, but dropped it as it seemed to cause strange aliasing/rounding errors, with lines
    not quite joining perfectly.

  • To enable easily modifying the SVGs with javascript, I removed them from iframes. This seemed to be okay with all examples I tried, because fonts are no longer turned into glyphs which could clash with each other. There may be some unknown edge case where isolation with iframes is still desirable.

You can see some generated output in my example notebook.
Notice how the text is now selectable, and the box moves when mousing over due to the javascript below.

My changes to the codebase can be found here DavidPowell@d5b25b6

I decided to create this issue before generating a PR, since after my changes there is definitely more
spaghetti code than I would like. Note that a lot of new options were needed to get the full benefit
of this approach, and some options (like the size) are now ignored. As far as I can tell the existing
functionality is preserved, but I haven't tested extensively.

Is this something that would make sense to integrate into ipython-tikzmagic?

The alternative would be for me to create a cleaned-up fork project which only has the dvisvgm mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions