jgm / typst-hs

Haskell library for parsing and evaluating typst
Other
43 stars 5 forks source link

Typst math mode ignored variables of some specific names #51

Closed memset0 closed 2 months ago

memset0 commented 2 months ago

Explain the problem.: When processing Typst source files, some specific variable names, such as bb, cannot be recognized in math mode (they are treated as blank). However, they work fine outside of math mode.

Pandoc version?: Pandoc 3.3, the online demo. From this link, you can also reproduce the bug.

An minimal example is as the following:

#let aa = $bold(a)$
#let bb = $bold(b)$
#let bbb = $bold(b)$

$f(X) = aa^T X bb$ #bb

$f(X) = aa^T X bbb$ #bbb

And the corresponding markdown (will also meet this bug when choose the other language) result:

$f(X) = \mathbf{a}^{T}X$ $\mathbf{b}$

$f(X) = \mathbf{a}^{T}X\mathbf{b}$ $\mathbf{b}$
jgm commented 2 months ago

Please give the expected output as well.

memset0 commented 2 months ago

Please give the expected output as well.

The excepted output is two same lines, both as same as the second line in the wrong output.

jgm commented 2 months ago

Minimal case:

#let bb = $bold(b)$

$bb$

currently yields an empty equation.

Moving to typst-hs, as this can be reproduced without pandoc.

jgm commented 2 months ago

Tested with typst.app. Behavior is a bit surprising!

$bb$

$cc$

$bb(X)$

#let bb=$3$

$bb$

$bb(X)$

yields

Screenshot 2024-08-05 at 11 16 40 PM
knuesel commented 2 months ago

I think that's expected: bb alone refers to the function (not text), typst shows the function name in monospace. If an argument is given the function is called. But after shadowing the function with the value 3, it's just 3 in both contexts.

jgm commented 2 months ago

Even more straightforward repro:

#let bb = 3

$bb$

should be '3' but is empty.

jgm commented 2 months ago

This works okay, so the problem seems specific to math mode:

#let emph = 3
#emph 
jgm commented 2 months ago

OK, I have a hypothesis. See this code https://github.com/jgm/typst-hs/blob/main/src/Typst/Evaluate.hs#L242-L260 When evaluating an equation we import the math module, and this clobbers the previous definition of bb.

jgm commented 2 months ago

I'd like to understand better how this is supposed to work.

For example, overline in math mode means math.overline; it doesn't mean what overline means outside of math mode.

The way I interpret that is that the implicitly imported math module takes precedence over prior bindings.

But apparently if you do #let bb = 3 outside the scope of the equation, this takes precedence over the implicitly imported math.bb.

I'm probably understanding something wrong. The typst documentation doesn't really clarify this.

knuesel commented 2 months ago

It's a bit under documented indeed... From what I understand you have summed it up already: in math mode the math functions shadow the standard functions, but both are shadowed by user-defined bindings defined in the current scope.

(By the way the standard functions will have their own std module in the next major release)