Create a function for math mode

How can I convert this

#let result(body) = {
rect()[#body]
}

$display(2(1-2x^2))/((1-2x^2)sqrt(1-x^2)) =
#result($display((2)/(sqrt(1-x^2)))$

in this

$display(2(1-2x^2))/((1-2x^2)sqrt(1-x^2)) =
result((2)/(sqrt(1-x^2))$

I want aaa to work the same as cancel.

When I use cancel, it keeps the text size; when I use my function, it doesn’t.

$ 2+3 / 5 = result( 23^2+2/4^2 ) $

$ 2 + 3/ 5 = cancel(23^2/42 + 3/2) } $

Hi

  • Please formulate the title of the topic as a question, so people will find it easier.
  • Please use ``` around your code examples so they will receive syntax highlighting.
  • Please check if the code snippets you post compile and don’t have mismatched parentheses or $.

See How to post in the Questions category


I think the problem is that rect (and box etc…) mess with math mode. (This is a known issue i think). I don’t really know of a workaround. However, you can probably achieve what you want using mannot – Typst Universe

2 Likes

To help other people help you, I reformulate your codes. Your original codes have mismatched (…) and $…$.

// the text size gets smaller
#let result(body) = rect(body)

$
  (2(1-2x^2)) / ((1-2x^2) sqrt(1-x^2)) =
  #result($2 / sqrt(1-x^2)$)
$

$
  (2(1-2x^2)) / ((1-2x^2) sqrt(1-x^2)) =
  result(2 / sqrt(1-x^2))
$

// it keeps the text size
$
  (2(1-2x^2)) / ((1-2x^2) sqrt(1-x^2)) =
  cancel(2 / sqrt(1-x^2))
$

3 Likes

When you insert a rect, block or box into the equation, and then an equation inside that, then you have a “math boundary” (let’s call it that) where the inner equation has no connection to the outer equation. So it loses its size information (the size context is that the outer equation is a block/display equation).

If you want to draw boxes inside equations, use package mannot:

#import "@preview/mannot:0.3.1": markrect 
$
  (2(1-2x^2)) / ((1-2x^2) sqrt(1-x^2)) =
  markrect(2 / sqrt(1-x^2))
$


If you wanted to use rect and equation, you’d have to tell the inner equation it’s also a block equation:

$
  (2(1-2x^2)) / ((1-2x^2) sqrt(1-x^2)) =
  std.rect(std.math.equation(2 / sqrt(1-x^2), block: #true))
$

(in this case using std. and not leaving math mode). I would prefer the first solution because it is easier to work with one equation instead of these nested equations (size, alignment, labels & references, etc).

7 Likes

Hi @andres.gimenez, thank you for your question. Unfortunately it is currently hard to understand. Could you please take the time and incorporate the feedback @nleanba gave you? They gave a good overview about how you could vastly improve your question. Keep in mind, ideally your question can help future visitors facing the same problem. The better you describe your problem, the more useful your question becomes.

1 Like