5
$\begingroup$

The two images show what I mean:

the tow images are generated with different size even if the code is the same

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?

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

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[
 TraditionalForm[
  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);

stylexpr = TraditionalForm[expr];

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

rasterize[stylexpr, 1200]
ImageDimensions@%

enter image description here

rasterize[stylexpr, 1200, 1]
ImageDimensions@%

enter image description here

stylexpr = 
  TraditionalForm[
   Style[expr, Bold, FontFamily -> "Chalkboard", Red]];
rasterize[stylexpr, 1200]
ImageDimensions@%

enter image description here

$\endgroup$
  • $\begingroup$ Yes, it goes to the right direction. Unfortunately it's in 10 and I have to run 9. I hope someone might suggest a workaround for it in 9. Anyhow, thanks for the solution. $\endgroup$ – bobknight Feb 20 '15 at 21:46
  • $\begingroup$ @Mr.Wizard: I read many other questions without having anything of similar, but it could be I missed the right one. $\endgroup$ – bobknight Feb 20 '15 at 21:48
  • $\begingroup$ @bobknight Unfortunately it's very easy to miss questions in the search. I find a web of linked questions often easier to navigate, hence my efforts with all the Related: links. $\endgroup$ – Mr.Wizard Feb 20 '15 at 21:49
  • $\begingroup$ @Mr.Wizard, thanks for the link, frankly I read that one but I thought it is a different situation. There the problem is due to Save As whereas I run Rasterize and it's not clear why the WindowSize of the working notebook should affect the final image breakdown. It seems there is a influence between RasterSize (but the same with ImageSize) and the WindowSize. And in the 6361 I cannot see any workaround suitable for Rasterize. I'll try to read it once again. $\endgroup$ – bobknight Feb 20 '15 at 21:59
  • $\begingroup$ @bobknight No, it's not a duplicate. Please don't waste your time. It's linked for other visitors who find this question and not that one, or the other way around. (I removed the "possible duplicate" from my comment after reading it again.) $\endgroup$ – Mr.Wizard Feb 20 '15 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.