mitex-rs / mitex

LaTeX support for Typst, powered by Rust and WASM. https://mitex-rs.github.io/mitex/
https://mitex-rs.github.io/mitex/tools/underleaf.html
Apache License 2.0
240 stars 8 forks source link

Nesting math and normal text in LaTeX is NOT correctly rendered #128

Closed xiaodong-hu closed 3 months ago

xiaodong-hu commented 4 months ago
#import "@preview/mitex:0.2.1": *
#set math.equation(numbering: "(1)", supplement: [#text(blue)[*Eq.*]])

#mitext(`
    \begin{align}
      \text{Stokes formula for Manifold $\mathcal M$}\quad \int_{\mathcal M}\mathrm{d}\omega&=\int_{\partial\mathcal M}\omega
    \end{align}
`)

Resulting 2024-02-15_16-34 where the nested math \mathcal M$ is not correctly rendered.

While

#import "@preview/mitex:0.2.1": *
#set math.equation(numbering: "(1)", supplement: [#text(blue)[*Eq.*]])

#mitext(`
    \begin{align}
      \text{Stokes formula for Manifold}~$\mathcal M$\quad \int_{\mathcal M}\mathrm{d}\omega&=\int_{\partial\mathcal M}\omega
    \end{align}
`)

is OK

OrangeX4 commented 4 months ago

Currently we convert \text{ xxx } to " xxx " instead of [ xxx ], this is to keep the starting and ending spaces not ignored by typst. As a result of this decision, we can't actually get into text mode inside \text{} at the moment, and there may not be a good way to do it at the moment.

Enivex commented 4 months ago

Currently we convert \text{ xxx } to " xxx " instead of [ xxx ], this is to keep the starting and ending spaces not ignored by typst. As a result of this decision, we can't actually get into text mode inside \text{} at the moment, and there may not be a good way to do it at the moment.

Would it be possible to convert the start and end of math to " as well? That is, \text{Some text $1+1=2 some more text} should become "Some text" 1+1=2 "some more text"

OrangeX4 commented 4 months ago

This could indeed be a workaround, I'll think about it later.

Enter-tainer commented 3 months ago

I wonder why we convert \text{} into strings. Would it make sense to convert it to a #text[]? This allows nested equations

xiaodong-hu commented 3 months ago

I can't agree more. Example:

#import "@preview/mitex:0.2.2": *

#mitex(`
\begin{equation}
    \text{Text around $\mathcal M$ symbol}
\end{equation}
`)

$ #text[Text around $cal(M)$ symbol] $

The space between text and math symbol is properly kept.

Enter-tainer commented 3 months ago

I wonder why we convert \text{} into strings. Would it make sense to convert it to a #text[]? This allows nested equations

This may turns out to be diffcult because it looks like it's beyond mitex's current abstract. It might not be too hard but still worth investigation. It looks like mitex correctly parse it.

    assert_debug_snapshot!(parse(r#"\text{Stokes formula for Manifold $\mathcal M$}"#), @r###"
    root
    |cmd
    ||cmd-name("\\text")
    ||args
    |||curly
    ||||lbrace'("{")
    ||||text(word'("Stokes"),space'(" "),word'("formula"),space'(" "),word'("for"),space'(" "),word'("Manifold"),space'(" "))
    ||||formula
    |||||dollar'("$")
    |||||cmd
    ||||||cmd-name("\\mathcal")
    ||||||args(word'("M"))
    |||||dollar'("$")
    ||||rbrace'("}")
    "###);
Enter-tainer commented 3 months ago

https://github.com/mitex-rs/mitex/blob/f2e9e31b80f24331964abd442f07df3d2f6402ce/crates/mitex/src/lib.rs#L442

Enter-tainer commented 3 months ago

Would it make sense if we enter text mode here and recursively convert all children in text?

OrangeX4 commented 3 months ago

That's what I'm going to do at the moment, and it doesn't seem necessary to care too much about the starting and ending spaces.