mgieseki / dvisvgm

A fast DVI, EPS, and PDF to SVG converter
https://dvisvgm.de
GNU General Public License v3.0
310 stars 34 forks source link

Added whitespace around tikz picture when svg is made using standalone class with dvisvgm option and a node #274

Open stertooy opened 2 months ago

stertooy commented 2 months ago

I'm not certain whether this is a bug with dvisvgm or with the standalone class, apologies if this does not belong here.

When using dvisvgm to make an SVG from the tex-code below, something odd happens: the svg gains a lot of whitespace to the left and to the bottom - as if dvisvgm draws it from the (0,0)-coordinate in TikZ, instead of where the actual image starts.

\documentclass[dvisvgm]{standalone}

\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}

\begin{tikzpicture}
    \draw (4,4) rectangle (8,6);
    \draw (6,5) node {a};
\end{tikzpicture}

\end{document}

tikz-faulty

This does not happen when the [dvisvgm] option is omitted from the standalone document class:

tikz-correct

Unfortunately, I've also run into instances before where this option is required to create a correct svg, so I can't just omit it entirely. Curiously enough, this only happens when a node is present in the TikZ-figure. If I comment out the line with the node, it works correctly whether or not the dvisvgm-option is present.

mgieseki commented 2 months ago

First of all, there's no need to apologize. Feel free to report any problems regarding dvisvgm here.

But in this case it's actually an issue with TikZ. It's the same as in #272. The letter a is positioned with DVI commands but also wrapped by literal SVG elements that shift it to the lower left. Since dvisvgm doesn't evaluate literal SVG fragments embedded in the DVI file, it doesn't know the final position and doesn't adapt the bounding box either. That's why it's too big.

When omitting option dvisvgm, TikZ embeds PostScript code which dvisvgm evaluates and therefore allows to compute a thight bounding box.

Long story short, this issue has to be fixed by the TikZ team.