Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I make a style definition which affects GraphicsBox, the style is used by Rasterize unless I specify an explicit ImageSize.

For example, I set a style for GraphicsLabel:

enter image description here

Then I can rasterize a plot and the defined style is used:

p = Plot[x, {x, 0, 1}];

enter image description here

But if I specify the ImageSize the default style is used instead:

Rasterize[p, ImageSize -> 200]

enter image description here

I would be interested to know why this happens and how I can work around it, i.e. rasterize at a specific size using the local style definitions.

share|improve this question
Would this work as you'd like? Rasterize[p, RasterSize -> 200]~Image~{ImageSize -> 200} – Rojo Nov 9 '13 at 19:49
@Rojo, not quite. That shrinks the labels - what I want is something that looks like Show[p, ImageSize -> 200] but in raster form. – Simon Woods Nov 9 '13 at 20:03
@Rojo, lol - I've just realised the workaround - Rasterize[Show[p, ImageSize -> 200]] – Simon Woods Nov 9 '13 at 20:05
It didn't shrink the labels in here, weird. But glad it triggered a solution over there, hehe – Rojo Nov 9 '13 at 20:05
This looks like a bug. Should this be tagged as bug? – Mike Honeychurch Nov 9 '13 at 20:56

In Mathematica 8.0.4 Rasterize[p] uses the default styles even without ImageSize.

The workaround is to specify the stylesheet explicitly:

p = Plot[x, {x, 0, 1}]
stylesheet = First@Options[EvaluationNotebook[], StyleDefinitions];
Rasterize[Style[p, stylesheet]]


share|improve this answer
This is a useful trick, but when I specify a reduced ImageSize in Rasterize I get the labels coming out demagnified. – Simon Woods Nov 10 '13 at 12:34
@Simon Please clarify what you mean saying that labels are demagnified? I currently work with v.8.0.4 and specifying reduced ImageSize gives the labels magnified down as expected, they do not still of the same absolute size in pixels as with the default ImageSize. It is what I expect and this behavior is correct from my point of view. If I enlarge ImageSize the absolute size of the labels is enlarged as I expect. What is confusing you? – Alexey Popkov Nov 10 '13 at 15:16
Alexey, the Properties & Relations section of the Rasterize help file shows what I mean. In the first picture the text stays the same size (i.e. the same absolute size in pixels) when the ImageSize is changed, this is the behaviour I wanted. In the second picture the text magnifies in proportion to the image size, this is also what happens with your code. Neither behaviour is incorrect, they are just different. – Simon Woods Nov 10 '13 at 17:16
I see your point, did not know this. In this case Graphics is embedded in a non-graphical construct (Style) and for this reason is magnified. – Alexey Popkov Nov 11 '13 at 7:04
up vote 2 down vote accepted

With Mathematica version 9.0.1 I can get the result I wanted by specifying the ImageSize as a Graphics option, e.g. using Show, rather than using it as an option to Rasterize:

Rasterize[Show[p, ImageSize -> 200]]

enter image description here

Note that the labels are still at the size specified in the style sheet.

Interestingly, the custom style is used if the graphic is embedded in a non-graphical construct, but in this case the font is demagnified. The following all produce the same output:

Rasterize[Style[p, {}], ImageSize -> 200]
Rasterize[Magnify[p, 1], ImageSize -> 200]
Rasterize[Text[p], ImageSize -> 200]

enter image description here

This is consistent with the documentation for Rasterize (Properties and Relations section) which states:

Rasterizing a graphic with a custom ImageSize option works by changing the graphic's ImageSize, not by magnifying the graphic. A graphic embedded in a non-graphical construct will magnify instead.

share|improve this answer
I suggested that before you came up with your solution, which I like better. But I am still intrigued why mine works for me. I'm using 9.0.1 on Windows. What about you? – Rojo Nov 10 '13 at 13:32
Ok, I tried it again and the result is something in between the default and custom font size. It rasterizes with the default image size but the custom font size, and then rescales ALL to the custom image size, which is different to what's intended: the font size shouldn't be rescaled. In conclusion, not a solution. – Rojo Nov 10 '13 at 13:52
@Rojo, okay - that's what I see too. – Simon Woods Nov 10 '13 at 14:06

Your Answer


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.