How do I add a rect within math mode that doesn't affect sizing?

Hello,

Let’s say I am trying to create a simple box with an outline around the fraction around this equation:

$x = 1/2$

This looks like the following:

However, if I add a rect, like so:

x = #rect(stroke: 1pt)[$1/2$]

It looks like this:

The fraction gets smaller. Is there any way to prevent this so the size of the content in the rect stays the same?

Thanks in advance.

I can only re-create your issue outside of inline math mode. If this is also true for you, this is caused because the math inside the rectangle is inlined, which uses a different size. You can fix this by setting the size to display:

$
x = #rect(stroke: 1pt)[$display(1/2)$]
$