6
$\begingroup$

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?

$\endgroup$
2
  • $\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
    Commented Jan 16, 2013 at 23:55
  • $\begingroup$ @Verbeia: Scaling with the window when, for example, its CSS is height:100%; width:100%. $\endgroup$
    – orome
    Commented Jan 17, 2013 at 0:24

2 Answers 2

3
$\begingroup$

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:

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

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
1
  • 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
    Commented May 27, 2013 at 6:50

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.