11
$\begingroup$

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:

enter image description here

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

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

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.

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

2 Answers 2

3
$\begingroup$

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]

image

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

output

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

image

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

image

$\endgroup$
4
  • $\begingroup$ This is a useful trick, but when I specify a reduced ImageSize in Rasterize I get the labels coming out demagnified. $\endgroup$ Commented Nov 10, 2013 at 12:34
  • $\begingroup$ @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? $\endgroup$ Commented Nov 10, 2013 at 15:16
  • $\begingroup$ 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. $\endgroup$ Commented Nov 10, 2013 at 17:16
  • $\begingroup$ 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. $\endgroup$ Commented Nov 11, 2013 at 7:04
3
$\begingroup$

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 (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.

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – Rojo
    Commented Nov 10, 2013 at 13:32
  • $\begingroup$ 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. $\endgroup$
    – Rojo
    Commented Nov 10, 2013 at 13:52
  • $\begingroup$ @Rojo, okay - that's what I see too. $\endgroup$ Commented Nov 10, 2013 at 14:06

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.