0
$\begingroup$

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.

$\endgroup$
8
  • 3
    $\begingroup$ Perhaps you can try Rasterize[Style[x^2+y^2,40], "RasterSize"]? $\endgroup$
    – kglr
    Commented Jun 9, 2023 at 13:02
  • 2
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Commented Jun 9, 2023 at 13:18
  • 1
    $\begingroup$ 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 $\endgroup$
    – Lukas Lang
    Commented Jun 9, 2023 at 15:44
  • 1
    $\begingroup$ @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). $\endgroup$
    – ihojnicki
    Commented Jun 10, 2023 at 0:20
  • 4
    $\begingroup$ Does this answer your question? How to get the size of a DateObject? Also: How can I get the box size of a BoxObject?. $\endgroup$
    – Domen
    Commented Jun 11, 2023 at 13:13

1 Answer 1

1
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Thank you, Domen has posted a link in the comment. That is close to my intention. $\endgroup$
    – imida k
    Commented Jun 11, 2023 at 22:48

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