Crates.io | math-core |
lib.rs | math-core |
version | 0.1.1 |
created_at | 2025-06-16 14:46:49.880056+00 |
updated_at | 2025-06-25 14:31:32.681298+00 |
description | Convert LaTeX equations to MathML Core |
homepage | |
repository | https://github.com/tmke8/math-core |
max_upload_size | |
id | 1714381 |
size | 461,944 |
math-core
allows you to convert LaTeX math to MathML Core, the MathML specification that is being implemented by web browsers. For example, this LaTeX code:
\sum_{i=0}^N x_i
is converted to
<math display="block">
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mi>N</mi>
</munderover>
<msub>
<mi>x</mi>
<mi>i</mi>
</msub>
</math>
which looks like this:
The goal of this project is to translate modern LaTeX math faithfully to the browser. More specifically, the goal is to…
This project is still in development, so not all LaTeX math commands that KaTeX supports are supported yet. See Development status below.
There are 4 ways to use the code in this project:
You can download precompiled binaries from the GitHub Release page. Alternatively, you can build the CLI binary from source:
cargo build --bin mathcore --release
You can see an explanation of the CLI interface with
mathcore --help
A config file can be used to define custom LaTeX macros. An example of such a file is contained in this repository: mathcore.toml
.
In the future, there may be more comprehensive documentation on a dedicated website.
Install the package with
pip install math-core
Basic documentation for the Python package can be found on pypi page.
Add to your project with
cargo add math-core
The documentation for the library can be found on docs.rs: https://docs.rs/math-core/latest/math_core/
The WebAssembly frontend is currently only used for the playground. In the future, a package for this may be published to npm.
The MathML code generated by this project is intended to be very portable and work without a CSS style sheet. However, in order to really get LaTeX-like rendering of the MathML, one unfortunately needs custom math fonts.
To specify the font, include something like this in your CSS:
@font-face {
font-family: Libertinus Math Regular;
src: url('./LibertinusMath-Regular.woff2') format('woff2');
}
math {
font-family: "Libertinus Math Regular", math;
}
Some day, perhaps, any font with a MATH table will be supported, but right now fonts require some tweaks.
The main problem is that Chromium does not look at ssty
variants when deciding on a glyph for super- and subscript (resulting in incorrectly rendered primes) and the fact that Chromium does not vertically center large operators and does not horizontally center accents. These problems have been manually fixed for the three fonts included in this project:
The fixes applied to the font files do not change the shape of any glyphs; they merely rearrange some glyphs or center them. The font files can be found in the playground/
directory in this repository. To use them in your website, download them here and load them on the page from your server. No guarantees will be made that the fonts on the playground will remain available on the current URLs.
The math fonts all have quite large font files. Especially New Computer Modern Math Book is enormous with an almost 700kB .woff2
file. Therefore, if possible, you should use font subsetting where the font file only includes those glyphs that are actually used on your website. Existing tools should work fine for this.
There are two tracking issues:
Other things that haven’t been implemented yet:
\text{...}
): https://github.com/tmke8/math-core/issues/431There are some things we will (most likely) never support.
\over
and \above
Supporting these would make the parser much more complicated, which does not seem worth it given that these commands are very rarely used and considered somewhat deprecated.
Other commands in this category: \choose
, \brace
, \brack
, \atop
\def
, \newcommand
, \definecolor
Again, supporting these would make the code much more complicated and anyway, these commands need to be repeated in every document. It seems more convenient to users and to the development of this project if new commands can only be defined in the config file.
\tag
This one is a bit debatable. It is a bit annoying to support this (because it introduces new state into the parser at a place where it’s currently stateless), but if there is strong demand for it, we could reconsider.
\mathit{012}
There is no Unicode range for this, so the only way to implement this would be with a custom font and a CSS class, which we would prefer to avoid.
\over
, \above
, \choose
, \brace
, \brack
, \atop
.:=
. Consider using \coloneqq
instead, which will result in the semantically correct Unicode symbol.x_{2}
vs x_2
. Both result in the same MathML because unnecessary groups are stripped away by this library.\hspace{1cm}
. It’s difficult to render them correctly. Instead, use relative length units like \hspace{2.8em}
.Note: at the time of this writing (June 2025), none of the following libraries render \vec
and \hat
correctly.
x_{2}
, resulting in an unnecessary <mrow>
in the output\mathcal
and \mathscr
.This code was originally forked from https://github.com/osanshouo/latex2mathml. The basic architecture of a lexer and a parser remains, but all the details have drastically changed and the supported portion of LaTeX commands has drastically increased.