Can Typst natively support LaTeX math?

There’s already a great library out there, mitex (@preview/mitex ), which lets us convert LaTeX math using its own functions. For example:

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

Write inline equations like #mi(`x`).

Also block equations:
#mitex(`
  \newcommand{\f}[2]{#1f(#2)}
  \f\relax{x} = \int_{-\infty}^\infty
    \f\hat\xi\,e^{2 \pi i \xi x}
    \,d\xi
`)

I’m wondering if it’s possible for Typst to natively understand LaTeX math syntax directly in the compiler.

// Global/local setting to enable LaTeX math parsing
#show math.equation: set latex
#show math.inline: set latex

// Then, use LaTeX syntax directly:
$\sum_{n=1}^\infty x^n$
\[
  \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
\]

And then, inside the Typst compiler, it automatically converts that LaTeX input into Typst’s own math structure, before rendering it. No external tools, no manual conversion – just type and go!

So, what do you think?

I think the issue comes down to two facts:

  • Using mitex already gives you at least 90% of what you want

  • To properly parse and handle LaTeX math, you need to implement a full LaTeX engine. This would be a huge effort and have very little shared with the Typst engine, as the two systems are different on fundamental levels.

    Notably, LaTeX works by way of macro expansion and not via pure functions. For example, the fraction in your example could be written also as \frac\sqrt\pi 2. So it is not clear simply after parsing, what is an argument for what. Instead, macros (\frac, \sqrt, \pi) can freely decide how many of the following tokens they consume as arguments, and they can permanently change how the rest is parsed. (E.g. a macro can replace the escape character from \ to whatever else)

2 Likes

Hi @Ahoy9841, welcome to the Typst forum!

Since Typst downloads packages automatically, I feel like we can already do something like this with no external tool / manual conversion:

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

#show raw.where(lang: "mitex", block: true): mitex
#show raw.where(lang: "mitex", block: false): mi

// Undo text size of default show rule for raw
#show raw.where(lang: "mitex"): set text(1em/0.8)

Write inline equations like ```mitex x```.

Also block equations:
```mitex
  \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
```

Having to use backticks instead of dollar signs is a minor inconvenience (and useful, as it allows to type both LaTeX math and Typst math). To make it less verbose you can replace mitex with a shorter language name. The missing syntax highlighting can be solved in the editor.

Having this in a third-party package makes sense to me: I don’t want the Typst developers to spend time dealing with LaTeX support, which will always be incomplete unless you include a whole TeX engine. It’s a perfect use case for a plugin like mitex.

Maybe one thing that could be improved is to offer a lighter syntax for the inline case: ```mitex x``` is a lot to write for $x$. Maybe support for `x`mitex could be added (which would reduce to `x`m if using m as the raw language).

(Edited to include @SillyFreak’s suggestion for correcting the text size.)

3 Likes

Thank you.

That’s exactly what I wanted to achieve!

Furthermore, it would be even better if the redefinition of the dollar symbol $ were allowed.

What sijo suggested works because raw elements contain just text that the Typst parser doesn’t touch in any way, so you can put TeX source code in there. No other element, including math equations ($...$) have that capability. They process their content into a more complex structure than just text, so by the time mitex can use it, it’s too late.

Implementing this would be a nonstarter; for example if you look at the syntax highlighting in your post:

\frac{\sqrt{\pi}}{2}

The \f is highlighted because it is an escape sequence (and since most(?) letters don’t have a special meaning in Typst, its result if f). So as far as Typst is concerned, you wrote f rac{s qrt{p i}}{2}. There’s basically no way to recover what you meant.

Just like you can’t type Python directly in Typst, you also can’t use LaTeX directly. Typst gives you hooks (namely, raw elements) to embed them, but embedding the syntax directly is not possible without compromising the design and vision of the language.

2 Likes

Note that this suffers from raw’s default show rule decreasing font size. Try this for comparison:

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

#show raw.where(lang: "mitex", block: true): mitex
#show raw.where(lang: "mitex", block: false): mi

Write inline equations like ```mitex x``` #mi(`x`).

Also block equations:
```mitex
  \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
```

#mitex(`
  \int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
`)

Since math uses its own font anyway, the following may be enough to counteract:

#show raw.where(lang: "mitex"): set text(1em/0.8)
3 Likes