# Rasterize is influenced by WindowSize of the notebook where I run it

The two images show what I mean:

Actually, the problem I have is that I need the images as in the second case, that's the whole expression in one row. In realty, the code is even more complex and is executed by the kernel calling UsingFrontEnd (it is under a webMathematica application) so, I cannot set manually the WindowSize as in the second example. If you want to try, the code is:

Rasterize[TraditionalForm[Log[Sqrt[x^2]](x - x^3)/(y + y^2) + 1/(y - y^2)],
"Image", RasterSize -> 800]


Run inside a notebook changing the window size and see the difference. I used Mathematica 9.0 but is seems to be the same with 10.0.2 Any workaround?

• I think you're looking for ImageSize -> 800. – Chip Hurst Feb 20 '15 at 21:19
• No, ImageSize has the same effect, it changes the size of the resulting image but the expression inside is still broken into two lines. – bobknight Feb 20 '15 at 21:45
• Related: (6361) – Mr.Wizard Feb 20 '15 at 21:48

Edit (23/02/15) : V10 & V9

## V10

It seems that the new (v10) option ImageFormattingWidth is what you are looking for.
In your case, I get the same graphic whatever the size of the window is if I write :

Rasterize[
Log[Sqrt[x^2]] (x - x^3)/(y + y^2) + 1/(y - y^2)], "Image",
RasterSize -> 800, ImageFormattingWidth -> Infinity]


As I understand from the docs, ImageFormattingWidth seems to play the same role than PageWidth which controls linebreaking. In order to prevent any linebreaking in the expression you want to Rasterize, it seems reasonable to set the option to Ìnfinity.

## V9

There is no ImageFormattingWidth option in Rasterize : linebreaking is done according the current WindowSize of the notebook. The workaround is then to Rasterize the object automatically in another notebook where WindowSize or PageWidth have been carefully chosen.

Here, I directly adapted the approach/code given by @szabolcs,@halirutan for a similar problem (see this post.)

In particular, instead of defining a WindowSize for the invisible Notebook where the expression will be rasterized, I just set PageWidth->Infinity to prevent any non expected linebreaking (like in v10, see above). Then, as @halirutan, I use Rasterize to rasterize directly the invisible notebook where was sent the object to be rasterized (i did not know it was possible to rasterize a notebook, see the applications section in the Rasterize docs.)

rasterize[myexpr_, mysize_, pad_: 0] :=
Module[{nb, mg},
nb = CreateDocument[ExpressionCell[myexpr, FontWeight -> Plain],
Visible -> False, PageWidth -> Infinity];
mg = Rasterize[nb, "Image",
RasterSize -> Switch[pad, 1, {mysize, {mysize}}, _, mysize]];
NotebookClose[nb];
Return@mg]


(the optional pad argument is to try to force Rasterize to get an image with the exact width size as requested. Actually I noticed that it will actually try do so by padding the image with extra space. See the example below)

Tests

expr = Log[Sqrt[x^2]] (x - x^3)/(y + y^2) + 1/(y - y^2);



I get the same images whatever the size of my working notebook window is.

rasterize[stylexpr, 1200]
ImageDimensions@%


rasterize[stylexpr, 1200, 1]
ImageDimensions@%


stylexpr =