# Rasterize uses default stylesheet if ImageSize is specified

Bug introduced in 8 or earlier and persisting through 11.0.1

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:

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

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


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

Rasterize[p, ImageSize -> 200]


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.

• 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. Nov 9 '13 at 20:03
• @Rojo, lol - I've just realised the workaround - Rasterize[Show[p, ImageSize -> 200]] 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? Nov 9 '13 at 20:56

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

\$Version

SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["GraphicsLabel"], FontColor -> RGBColor[0, 1, 0], FontWeight -> Bold,
FontSize -> 16]}, StyleDefinitions -> "PrivateStylesheetFormatting.nb"]];

"8.0 for Microsoft Windows (64-bit) (October 7, 2011)"

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


Surprisingly, if we simply wrap Graphics by Style the Notebook's stylesheet is applied (wrapping by Pane, Text or Row also work):

 {Rasterize[Style[p]], Rasterize[Style[p], ImageSize -> 360]}


Unfortunately specifying the stylesheet via Style doesn't work (checked with versions 8.0.4 and 11.0.1):

SetOptions[EvaluationNotebook[], StyleDefinitions -> Inherited]
Rasterize[Style[p,
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["GraphicsLabel"], FontColor -> RGBColor[0, 1, 0], FontWeight -> Bold,
FontSize -> 16]}, StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]]


But we can specify it as an option of Notebook expression:

Rasterize[Notebook[{Cell[BoxData@ToBoxes@p, "Output"]},
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["GraphicsLabel"], FontColor -> RGBColor[1, 0, 0], FontWeight -> Bold,
FontSize -> 16]}, StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]]


• This is a useful trick, but when I specify a reduced ImageSize in Rasterize I get the labels coming out demagnified. 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? 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. 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. Nov 11 '13 at 7:04

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


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]


This is consistent with the documentation for Rasterize (the first two items under the "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.

• 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. Nov 10 '13 at 14:06