0

Is there a method to measure (in pixel) width and height of an expression without making it to an Image ?

I found a method for measuring width and height of an expression in this forum,
but it uses Rasterize and ImageDimensions.
It means it uses unnecessary CPU or RAM resources.
Also it is less intuitive. Moreover, I think MMA FrontEnd already knows the answer!

enter image description here

If you double click the expression, MMA FrontEnd highlights the boundary of the expression in blue.
It means MMA FrontEnd knows the answer before making it into an image.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
imida k
  • 4,285
  • 9
  • 17
  • 3
    Perhaps you can try Rasterize[Style[x^2+y^2,40], "RasterSize"]? – kglr Jun 09 '23 at 13:02
  • Thank you! It seems to be the document that suits my intention. Just before, I've tested with Timing. The test compares the performance of "RasterSize" option technique VS Rasterize + ImageDimensions technique, and there was little difference. So it may be appropriate to conclude there is no fast way to measure width/height without (essentially) making it into an image. – imida k Jun 09 '23 at 13:16
  • 2
    Perhaps not so useful a distinction, but I think it's the front end (rather than the kernel) that is responsible for visualization and that "knows the answer" you seek. – MarcoB Jun 09 '23 at 13:18
  • @MacroB, Thank you! I edited it. – imida k Jun 09 '23 at 13:21
  • 1
    It's not really fair to say that the front-end knows the size before "making it into an image": The front-end effectively had to rasterize the expression in order to display it in the first place – Lukas Lang Jun 09 '23 at 15:44
  • 1
    @LukasLang, the FrontEnd knows the dimensions of the boxes in points. It also knows the density of the display that the notebook is on. So yes, it knows the number of pixels. It will never directly report them though, since they are not portable (even on the same machine). – ihojnicki Jun 10 '23 at 00:20
  • 4
  • Thank you, there was a way to do it! – imida k Jun 11 '23 at 22:49

1 Answers1

1

On my system ("13.1.0 for Mac OS X ARM (64-bit) (June 16, 2022)") the command that @kglr suggested runs in 0.025 seconds.

RepeatedTiming[Rasterize[Style[x^2 + y^2, 40], "RasterSize"]]

How much speed up is required? I wonder if you could perhaps describe your problem in full. I suppose you have to find out the size of millions of formulas. Maybe some latex (you can post your question of tex.stackexchange.com) solution can be of help?

yarchik
  • 18,202
  • 2
  • 28
  • 66