6 releases

0.2.3 Apr 27, 2020
0.2.2 Apr 21, 2020
0.1.1 Apr 6, 2020

#152 in Science

Download history 210/week @ 2023-11-25 165/week @ 2023-12-02 245/week @ 2023-12-09 155/week @ 2023-12-16 186/week @ 2023-12-23 109/week @ 2023-12-30 210/week @ 2024-01-06 232/week @ 2024-01-13 132/week @ 2024-01-20 135/week @ 2024-01-27 190/week @ 2024-02-03 252/week @ 2024-02-10 178/week @ 2024-02-17 146/week @ 2024-02-24 138/week @ 2024-03-02 131/week @ 2024-03-09

653 downloads per month
Used in 10 crates (7 directly)

MIT license

80KB
1.5K SLoC

latex2mathml

Crate docs.rs

latex2mathml provides a functionality to convert LaTeX math equations to MathML. This crate is implemented in pure Rust, so it works in any environments if Rust works (including WebAssembly).

Supported LaTeX commands

  • Numbers, e.g. 0, 3.14, ...
  • ASCII and Greek (and more) letters, e.g. x, \alpha, \pi, \aleph, ...
  • Symbols, e.g., \infty, \dagger, \angle, \Box, \partial, ...
  • Binary relations, e.g. =, >, <, \ll, :=, ...
  • Binary operations, e.g. +. -, *, /, \times, \otimes, ...
  • Basic LaTeX commands, e.g. \sqrt, \frac, \sin, \binom, ...
  • Parentheses, e.g., \left\{ .. \middle| .. \right], ...
  • Integrals, e.g., \int_0^\infty, \iint, \oint, ...
  • Big operators, e.g., \sum, \prod, \bigcup_{i = 0}^\infty, ...
  • Limits and overset/underset, e.g., \lim, \overset{}{}, \overbrace{}{}, ...
  • Font styles, e.g. \mathrm, \mathbf, \bm, \mathit, \mathsf, \mathscr, \mathbb, \mathfrak, \texttt.
  • White spaces, e.g., \!, \,, \:, \;, \ , \quad, \qquad.
  • Matrix, e.g. \begin{matrix}, \begin{pmatrix}, \begin{bmatrix}, \begin{vmatrix}.
  • Multi-line equation \begin{align} (experimental, see below).
  • Feynman slash notation: \slashed{\partial}.

See examples/equations.rs for examples. Note that all supported commands are defined in src/token.rs.

Unsupported LaTeX commands

  • New line \\, except for ones in a matrix or align environment.
  • Alignment &, except for ones in a matrix or align environment.
  • Complicated sub/superscripts (<mmultiscripts>).

Align environment \begin{align} .. \end{align} is experimentally supported from version 0.2.1, as suggested in the issue #2. Because it is implemented using matrix environment, the output MathML is not recommended and rendered equation may be not well-formatted.

Dollar sign \$ is allowed for the latex_to_mathml function, but the replace function does not allow it. This is because the replace function assumes all dollar signs appear as boundaries of LaTeX equations.

If a feature you need is lacked, feel free to open an issue.

Usage

For a single LaTeX equation:

use latex2mathml::{latex_to_mathml, DisplayStyle};

let latex = r#"\erf ( x ) = \frac{ 2 }{ \sqrt{ \pi } } \int_0^x e^{- t^2} \, dt"#;
let mathml = latex_to_mathml(latex, DisplayStyle::Block).unwrap();
println!("{}", mathml);

For a document that includes LaTeX equations:

let text = r#"
Let us consider a rigid sphere (i.e., one having a spherical 
figure when tested in the stationary system) of radius $R$ 
which is at rest relative to the system ($K$), and whose centre 
coincides with the origin of $K$ then the equation of the 
surface of this sphere, which is moving with a velocity $v$ 
relative to $K$, is
$$\xi^2 + \eta^2 + \zeta^2 = R^2$$
"#;
let mathml = latex2mathml::replace(text).unwrap();
println!("{}", mathml);

To convert HTML files in a directory recursively, use latex2mathml::convert_html. This function is for converting HTMLs generated by cargo doc.

See also examples/equations.rs and examples/document.rs.

No runtime deps