5
$\begingroup$

I'm trying to export a graph as a graphic to an SVG file with transparent background. When the background is specified as a color (or unspecified), it works just fine. When the background is specified as None, it offsets the graphic down and to the right a bit and ends of cutting off part of the graphic.

g = CompleteGraph[7];
g = GraphicsGrid[{{g}}];
Export["good.svg", g, Background -> Pink];
Export["bad.svg", g, Background -> None];

Transparency works perfectly as expected when the graphic is exported to PNG. Is this a bug, or am I just missing something here?

I'm running Mathematica 9.0.0.0 on Linux 64-bit.

Here's a screenshot of what I am seeing when I open the bad.svg in the gnome image viewer (with some style changes made to the graph for visibility). Note that the image is cut off on the bottom and on the right. When I open bad.svg in chromium or in firefox, it appears fine.

1

$\endgroup$
1
  • $\begingroup$ Can you please detail your OS and Mathematica version? $\endgroup$
    – Yves Klett
    Commented Jun 30, 2014 at 5:18

1 Answer 1

6
$\begingroup$

Although I do see a difference between the contents of bad.svg when I export with version 8.0.4 and 9.0.1, the svg is displayed in exactly the same way by Google Chrome. The display does indeed show a white background in Safari, though, when exported from Mathematica 8.0.4 on OS X. So we could now argue whether this is the fault of the browser, or the fault of Mathematica. I think it is indeed a problem with Mathematica's Export in version 8.0.4.

What happens is that there is an unnecessary line in the exported SVG file, located at a nominally inaccessible position:

<rect fill='rgb(100%,100%,100%)' stroke='none' x='-2.2518e+15' y='-2.2518e+15' width='4.5036e+15' height='4.5036e+15' />

This is apparently recognized by Google Chrome as something that can't be displayed. But in Safari, it is in fact displayed as a white background. I checked this by embedding the file in an HTML document with

<body style="background:gray;"><img src="bad.svg">

When I remove the offending rect from the SVG file, the display in Safari is fixed:

bad good

Since I can't be sure that this faulty line is always generated the same way when exporting SVG with no background, I can at this point only recommend removing this line from the SVG file manually. It's line 6 in the exported file.

In version 9.0.1, I don't see this line in the exported file, and there is no white background in any browser that I have tried.

Edit to address sizing

As mentioned in the comment, there is a problem in that the size of the exported SVG isn't always correctly identified. To fix this, you can use the function svgExport from my answer to "Embed Code Into Image". It makes the necessary replacements in the exported code.

$\endgroup$
3
  • $\begingroup$ For me, the transparency works fine in both google-chrome and firefox when I embed it into an HTML file, but the image is cut off again in firefox (much more so than in the gnome image viewer screenshot above). Also, using Mathematica 9.0.0.0, my Bad.SVG file doesn't have a line like you describe. There is not line starting with <rect fill= but instead on line 6 I've got <polyline fill=. Link the the SVG here. $\endgroup$ Commented Jun 30, 2014 at 6:08
  • $\begingroup$ I agree about Firefox, but it can be easily fixed with <img src="bad.svg" style="width:360px;height:360px;". It's unrelated to the background issue, see here. So basically, one should call this a bug that has apparently been fixed in version 9.0.1 but not in 9.0.0. $\endgroup$
    – Jens
    Commented Jun 30, 2014 at 16:42
  • $\begingroup$ For the Firefox problem, I added a link to my answer that fixes this issue. $\endgroup$
    – Jens
    Commented Jun 30, 2014 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.