6个版本
| 0.2.3 | 2020年4月27日 |
|---|---|
| 0.2.2 | 2020年4月21日 |
| 0.1.1 | 2020年4月6日 |
#185 in 科学
677 每月下载量
在 11 个Crate中使用了 (8 直接)
80KB
1.5K SLoC
latex2mathml
latex2mathml 提供将LaTeX数学方程式转换为MathML的功能。此crate是用纯Rust实现的,因此如果Rust可用(包括WebAssembly),则可在任何环境中工作。
支持的LaTeX命令
- 数字,例如
0,3.14,... - ASCII和希腊(以及更多)字母,例如
x,\alpha,\pi,\aleph,... - 符号,例如,
\infty,\dagger,\angle,\Box,\partial,... - 二元关系,例如
=,>,<,\ll,:=,... - 二元运算,例如
+,-,*,/,\times,\otimes,... - 基本LaTeX命令,例如
\sqrt,\frac,\sin,\binom,... - 括号,例如,
\left{ .. \middle| .. \right],... - 积分,例如,
\int_0^\infty,\iint,\oint,... - 大运算符,例如
\sum,\prod,\bigcup_{i = 0}^\infty,... - 极限和上标/下标,例如
\lim,\overset{}{}{}{},\overbrace{}{}{}{},... - 字体样式,例如
\mathrm,\mathbf,\bm,\mathit,\mathsf,\mathscr,\mathbb,\mathfrak,\texttt。- MathML缺少草书数学变体:https://github.com/mathml-refresh/mathml/issues/61
- 空白符,例如
!,,,:,;,\,\quad,\qquad。 - 矩阵,例如
\begin{matrix},\begin{pmatrix},\begin{bmatrix},\begin{vmatrix}。 - 多行方程
\begin{align}(实验性,见下文)。 - 费曼斜线符号:
\slashed{\partial}。
有关示例,请参阅 examples/equations.rs。注意,所有支持的命令均定义在 src/token.rs 中。
不支持的LaTeX命令
- 换行符
\\,除了矩阵或align环境中的那些。 - 对齐符
&,除了矩阵或align环境中的那些。 - 复杂的上标/下标(
<mmultiscripts>)。
align环境 \begin{align} .. \end{align} 从版本 0.2.1 开始实验性支持,如 问题 #2 中建议。因为它使用 matrix 环境实现,所以输出的MathML不推荐,渲染的方程可能格式不好。
美元符号 $ 允许用于 latex_to_mathml 函数,但 replace 函数不允许。这是因为 replace 函数假定所有美元符号都出现在LaTeX方程的边界。
如果您缺少所需的功能,请随时提出问题。
用法
对于单个LaTeX方程
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);
对于包含LaTeX方程的文档
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);
要递归地转换目录中的HTML文件,请使用 latex2mathml::convert_html。此函数用于转换由 cargo doc 生成的HTML。
请参阅 examples/equations.rs 和 examples/document.rs。