How can two elements be equal and not print the same way?

Hello,

I am puzzled by the following code :

#let a = $accent(italic(M),arrow)$
#let b = $cal(M)$

#(a.body.base == b.body) // returns true

$#a.body.base$ // "normal" M

$#b.body$ // calligraphic M

Isn’t it weird that equal elements produce different results ?

2 Likes

You’re comparing inside code mode, but the output you provide as evidence is in maths.

Notice the difference:

#let a = $accent(italic(M), arrow)$
#let b = $cal(M)$

#(a.body.base == b.body) // returns true

a.body.base // "normal" M

b.body // calligraphic M

They’re the same character when you put present them in this way.

I get where you’re coming from though, calligraphic characters in maths can often be achieved in different ways while having a similar style.

Comparing the equations also gives true:

#let a = $accent(italic(M), arrow)$.body.base
#let b = $cal(M)$.body

#(a == b) // → true

#(math.equation(a) == math.equation(b)) // → true

#math.equation(a) #math.equation(b) // → two different-looking M

I think its a reasonable assumption to make that if you have two things that compare equal that they also render equal (unless we change the context between them)

In this specific case, a and b are styled() elements, and it appears as though == does not consider the style. I would consider this a bug with Typst if I’m honest.

This can also be reproduced without any math mode (so its not math-mode-specific weirdness):

#let a = [#set text(font: "Clicker Script");M]
#let b = [#set text(weight: 800);M]

#(a == b) // → true

#a #b // → two different-looking  M

In my opinion, it should not be possible to define a and b such that the following code

+ `#a` is #a.
+ `#(a == b)` is #(a == b).
+ But `#b` is #b.

has this output:

but it is:
#let a = [#set text(size: 1em);normal]
#let b = [#show "normal": it => "not normal and contains a hidden message";normal]

In either case, as a workaround you can use a.child/b.child (i.e. a.body.base.child/b.body.child in the original example) to gain access to the raw, unstyled M

3 Likes

Thank you!
However, I would like to distinguish between two different styles. Is there any way to access the style programmatically ?
To be clear, I am looking for a function that would return false for a and b and true for a and a.
I also thought about comparing directly the source code of the equations but I believe there is no way to retrieve the source code of a content in code mode.

Sadly, I don’t think this is possible :(

There is a .styles property, but it only renders as .. and always compares as not equal (notably, a.styles == a.styles is false.)

1 Like

Ok, thanks anyway. I will have to think to an alternative way to achieve my goal.