# How can I get Mathematica to include size information in exported SVG?

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?

-
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 at 23:55
@Verbeia: Scaling with the window when, for example, its CSS is height:100%; width:100%. –  raxacoricofallapatorius Jan 17 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:

<!DOCTYPE HTML>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>SVG scaling</title>

<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:

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.
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 at 6:50