9
$\begingroup$

In the past, I was able to export Mathematica plots as SVG files with relative ease. However, when I tried to do this recently, I found that the text in the plot axes was rendered completely illegible, with all the characters being much too large, and all characters in a text element piling up on one another. Here is an screenshot of such a mangled plot: enter image description here

The SVG file was produced with the following command:

Export["~/Desktop/test-m10-1.svg",
 Plot[Sin[x], {x, 0, 10}]]

While I don't have Mathematica 10.0 installed on this machine, I do have Mathematica 9, and this command works correctly in 9. Looking at the generated SVG as XML, it looks like the "text" elements that represent the labels on the axes in the SVG generated by M9 have somehow been replaced by "symbol" elements in M10.1.

FWIW, this was done on a machine running Mac OS 10.9.5.

EDIT: I tried using this svgExport routine, and it resulted in an SVG file which had the axes and tick labels displaying correctly, but didn't actually show the curve.

$\endgroup$
  • $\begingroup$ I reproduced in V10.1 for Mac 10.10.2 $\endgroup$ – Murta Apr 20 '15 at 18:30
  • $\begingroup$ reproduced in V10.1 for Mac 10.10.3 as well. $\endgroup$ – sunt05 Apr 20 '15 at 20:44
  • $\begingroup$ I don't have version 10.1, but would be curious if you also have the same issue using my export function in this answer. It looks like a problem with missing box dimensions of some kind, but I don't have an SVG file that shows the issue, so I can only guess... In the linked answer I added more explicit box dimensions to the exported SVG file, so maybe that helps here, too. $\endgroup$ – Jens Apr 21 '15 at 0:29
  • $\begingroup$ I can't reproduce in V10.1 for Windows 7. A browser issue? $\endgroup$ – Verbeia Apr 21 '15 at 1:38
  • $\begingroup$ @Verbeia nope, the structure of exported svg file is different on Mac and Windows. Just an observation, I don't much about svg. But it seems that the export makes a difference. $\endgroup$ – Kuba Aug 10 '15 at 9:01
6
$\begingroup$

After some experimentation, I discovered that the issue preventing svgExport from working was that string replacements which add attributes caused multiple copies of the width and height attributes to be inserted. To avoid that, I rewrote it to use rule replacements on the symbolic representation of the XML instead of string replacement. The result is rather more verbose, but it's also more robust:

roundtripPDF[gr_, opts : OptionsPattern[]] :=
  First@ImportString@ExportString[gr, "PDF", opts];

asXML[gr_] := 
  ImportString[ExportString[gr, "SVG"], "XML", "ReadDTD" -> False];

addComment[None, elt_] := elt;
addComment[Automatic, elt_] := 
  addComment["*** Exported from Mathematica ***", elt];
addComment[comment_String, XMLElement[tag_, attrs_, contents_]] :=
  XMLElement[tag, attrs, 
   Append[contents, XMLObject["Comment"][comment]]];

addAttributes[oldAttrs_, newAttrs_] := 
  DeleteDuplicatesBy[Join[oldAttrs, newAttrs], First];

Options[Pillsy`ExportSVG] := {Background -> Automatic, 
   AspectRatio -> Automatic, "CommentString" -> Automatic};

Pillsy`ExportSVG[file_String, expr_, opts : OptionsPattern[]] :=
 With[{
   svgStructure = asXML@roundtripPDF[expr, opts],
   dims = 
    ToString /@ 
     ImageDimensions@
      Rasterize[Show[expr, ImagePadding -> 0], "Image"]
   },
  Export[
   file,
   svgStructure /.
    XMLElement["svg", attrs_, contents_] :>
     addComment[
      OptionValue["CommentString"],
      XMLElement["svg",
       addAttributes[attrs,
        Join[
         {"viewBox" -> "0 0 " <> StringRiffle[dims],
          "width" -> dims[[1]] <> "pt",
          "height" -> dims[[2]] <> "pt"},
         If[OptionValue[AspectRatio] === Full,
          {"preserveAspectRatio" -> "none"},
          {}]]],
       contents]], "XML"]]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ works for me on 10.3 $\endgroup$ – user3240588 Jan 12 '16 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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