Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  Verbeia Jan 16 '13 at 23:55
    
@Verbeia: Scaling with the window when, for example, its CSS is height:100%; width:100%. –  raxacoricofallapatorius Jan 17 '13 at 0:24

2 Answers 2

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.

share|improve this answer
2  
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>. –  Jens May 27 '13 at 6:50

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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