When I use Export to export graphics in SVG format, the exported file contains no width height or viewBox attributes, so that the resulting files don't "scale" at all, which is the whole point.

Is there a way to get Export to include size information in exported SVGs?

  • $\begingroup$ I'm not sure what you mean by "scale". If I open an SVG exported from Mathematica in IE9, it definitely "scales" when I zoom up and down using the mousewheel. Did you have something other than that in mind? $\endgroup$ – Verbeia Jan 16 '13 at 23:55
  • $\begingroup$ @Verbeia: Scaling with the window when, for example, its CSS is height:100%; width:100%. $\endgroup$ – orome Jan 17 '13 at 0:24

I just answered a related question where I posted an SVG export function that fixes the scaling problems. At least that's what I conclude from the following tests:

  1. First load the definitions for svgExport from the answer linked here.

  2. Create a plot and export it like this:

    p = Plot[Sin[x], {x, 0, 2 Pi}];    
    svgExport["plot.svg", p, AspectRatio -> Full]    
    (* ==> "plot.svg" *)
  3. Create a HTML file that embeds the SVG as shown:

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>SVG scaling</title>
        <h1>SVG scaling</h1>
    <object style="width:400px;height:400px;" data="plot.svg" type="image/svg+xml"></object>

The page displays as follows in a browser:

screen shot

The scaling is completely determined by the width and height values in the <object> tag. If you leave out the AspectRatio option in svgExport, the overall scaling still works based on the <object> settings, but the aspect ratio of the graphics is always preserved so that there will be additional white space.


Mathematica does not put information about width and height into <svg> tag, but the first group contains a filled rectangle (<rect fill ...>) that does have width and height.

You may extract width and height attributes from that <rect> and assign them to the root <svg> element.

  • 2
    $\begingroup$ This doesn't generally work. It relies on a <rect> that only gets created when the graphic is exported with default background. If you add the Export option Background -> None there is no such <rect>. $\endgroup$ – Jens May 27 '13 at 6:50

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.