GithubHelp home page GithubHelp logo

latex2mathml's Introduction

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.

latex2mathml's People

Contributors

osanshouo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

latex2mathml's Issues

\right| should not be converted to∥

Thanks for the library! I ran a few tests on the latest version (0.2.3) and noticed that:

\left|12\cdot\left(3+54\right)^2-9\right\|

is converted to

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mo stretchy="true" form="prefix">|</mo><mrow><mn>12</mn><mo>·</mo><msup><mrow><mo stretchy="true" form="prefix">(</mo><mrow><mn>3</mn><mo>+</mo><mn>54</mn></mrow><mo stretchy="true" form="postfix">)</mo></mrow><mn>2</mn></msup><mo>-</mo><mn>9</mn></mrow><mo stretchy="true" form="postfix">∥</mo></mrow></math>

Any ideas why the second absolute value bar is converted to the ∥?

Multi-line expression suggestion for align

Multi-line expressions, e.g. \begin{align}..\end{align}. These are unsupported LaTeX commands for this project.
Can you add an option for replacing "align" to "matrix"?
This will help to show the code, not perfect, but good.

Spaces in `mtext` tag are removed

The input \frac{\text{number of apples}}{\text{number of students}} generates the following output:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mfrac><mtext>numberofapples</mtext><mtext>numberofstudents</mtext></mfrac></math>

Spaces should not be removed in this case.

Latex Cases Support for Piecewise Equations

Presently, the following latex causes latex2mathml to throw the following error:
UnknownEnvironment("cases")

Here is the latex in question that it throws the error on:

$$ \frac{\partial S_{i,j}}{\partial z_{i,k}} = 
\begin{equation}
	\begin{cases}
		S_{i,j}\cdot(1-S_{i,k}) , & \text{j=k} \\ \\
		-S_{i,j} \cdot S_{i,k} , & j \ne k 
	\end{cases}
\end{equation} $$

Support for this feature would be awesome as it'd support piecewise equations.

Use character entities for encoding output

While doing experiments with the library, we noticed that

\left|2x-4\right|<=14

is encoded to:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow><mo stretchy="true" form="prefix">|</mo><mrow><mn>2</mn><mi>x</mi><mo>-</mo><mn>4</mn></mrow><mo stretchy="true" form="postfix">|</mo></mrow><mo><</mo><mo>=</mo><mn>14</mn></math>

While this may work, in some cases, like ours, it can create problems for downstream components which expect html character encodings (so the output should match):

<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mrow><mo stretchy="true" fence="true" form="prefix">&#x0007C;</mo><mrow><mn>2</mn><mi>x</mi><mo>&#x02212;</mo><mn>4</mn></mrow><mo stretchy="true" fence="true" form="postfix">&#x0007C;</mo></mrow><mo>&lt;</mo><mo>&#x0003D;</mo><mn>14</mn></mrow></math>

It would be nice to have an option to switch this encoding on. It would be very helpful to get a pointer on where to implement such a feature.

Using superscript and subscript simultaneously

To replicate, try either:

  1. \alpha_{2}^{4}
  2. \alpha^{4}_{2}

You will observe that they don't render as they should. The subscripts and superscripts should both apply directly to \alpha, not to each other; the two expressions should render identically.

braket.sty

braket.sty defines \bra, \ket, \braket, \Braket commands. Should they be implemented in this crate?

It is useful for quantum mechanics and statistics, but for example \ket{\psi} can be replaced to \left| \psi \right\rangle.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.