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!
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)
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.)
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.