mitex-rs/mitex

Nesting math and normal text in LaTeX is NOT correctly rendered

Closed this issue ยท 9 comments

#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

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.

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"

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

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

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.

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'("}")
    "###);

if typst_name.starts_with("text") {

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

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.