pub struct MathFormatter;
impl MathFormatter {
pub fn new() -> Self {
Self
}
pub fn format(math: &str) -> String {
let mut result = math.to_string();
result = Self::convert_old_fractions(&result);
result = Self::format_matrices(&result);
result = Self::format_sqrt(&result);
result = Self::format_fractions(&result);
result = crate::math::symbols::replace_math_symbols(&result);
result = Self::format_math_alphabets(&result);
result = Self::format_accents(&result);
result = Self::format_superscripts(&result);
result = Self::format_subscripts(&result);
result
}
pub fn replace_sqrt_in_expression(expr: &str) -> String {
crate::math::radicals::format_sqrt(expr, Self::format)
}
fn format_matrices(text: &str) -> String {
let mut result = String::new();
let mut index = 0;
const MATRIX_ENVS: &[(&str, char, char)] = &[
("pmatrix", '(', ')'),
("bmatrix", '[', ']'),
("Bmatrix", '{', '}'),
("vmatrix", '|', '|'),
("Vmatrix", '\u{2016}', '\u{2016}'), ("matrix", '\0', '\0'), ("smallmatrix", '\0', '\0'), ];
while index < text.len() {
let remaining = &text[index..];
let mut matched = false;
for (env, open, close) in MATRIX_ENVS {
let begin = format!("\\begin{{{env}}}");
let end = format!("\\end{{{env}}}");
if remaining.starts_with(&begin) {
if let Some(rel_end) = Self::find_matching_end(remaining, &begin, &end) {
let body = &remaining[begin.len()..rel_end];
let formatted_body = Self::format_matrices(body);
if *open != '\0' {
result.push(*open);
}
result.push_str(&formatted_body);
if *close != '\0' {
result.push(*close);
}
index += rel_end + end.len();
matched = true;
break;
}
}
}
if matched {
continue;
}
let ch = remaining
.chars()
.next()
.expect("remaining is never empty while index < text.len()");
result.push(ch);
index += ch.len_utf8();
}
result
}
fn find_matching_end(text: &str, begin: &str, end: &str) -> Option<usize> {
let mut depth = 0;
let mut search_from = 0;
while let Some(rel) = text[search_from..].find('\\') {
let pos = search_from + rel;
if text[pos..].starts_with(begin) {
depth += 1;
search_from = pos + begin.len();
} else if text[pos..].starts_with(end) {
depth -= 1;
if depth == 0 {
return Some(pos);
}
search_from = pos + end.len();
} else {
search_from = pos + 1;
}
}
None
}
fn find_enclosing_braces(text: &str, kw_start: usize, kw_len: usize) -> Option<(usize, usize)> {
let bytes = text.as_bytes();
let mut depth = 0;
let mut open = None;
let mut i = kw_start;
while i > 0 {
i -= 1;
let ch = bytes[i];
if ch == b'}' {
depth += 1;
} else if ch == b'{' {
if depth == 0 {
open = Some(i);
break;
}
depth -= 1;
}
}
let open = open?;
let mut depth = 0;
let mut close = None;
let mut i = kw_start + kw_len;
while i < bytes.len() {
let ch = bytes[i];
if ch == b'{' {
depth += 1;
} else if ch == b'}' {
if depth == 0 {
close = Some(i);
break;
}
depth -= 1;
}
i += 1;
}
Some((open, close?))
}
fn convert_old_fractions(text: &str) -> String {
let ops: &[(&str, &str)] = &[
("\\over", "\\frac"),
("\\choose", "\\binom"),
("\\brack", "\\binom"),
("\\brace", "\\binom"),
];
let mut result = text.to_string();
loop {
let mut earliest: Option<(usize, &str, &str)> = None;
for (op, target) in ops {
let mut search_from = 0;
while let Some(rel) = result[search_from..].find(op) {
let pos = search_from + rel;
let after = pos + op.len();
let next_is_alpha = result
.get(after..)
.and_then(|s| s.chars().next())
.is_some_and(|c| c.is_alphabetic());
if !next_is_alpha {
if earliest.is_none_or(|(p, _, _)| pos < p) {
earliest = Some((pos, op, target));
}
break;
}
search_from = pos + 1;
}
}
let (pos, op, target) = match earliest {
Some(e) => e,
None => break,
};
let (open, close) = match Self::find_enclosing_braces(&result, pos, op.len()) {
Some(b) => b,
None => break, };
let num = result[open + 1..pos].trim();
let den = result[pos + op.len()..close].trim();
let replacement = format!("{target}{{{num}}}{{{den}}}");
result.replace_range(open..close + 1, &replacement);
}
result
}
fn format_sqrt(text: &str) -> String {
crate::math::radicals::format_sqrt(text, Self::format)
}
fn format_fractions(text: &str) -> String {
crate::math::fractions::format_fractions(text, Self::format)
}
#[allow(clippy::type_complexity)]
fn format_math_alphabets(text: &str) -> String {
let mut result = String::new();
let mut index = 0;
let alphabets: [(&str, fn(char) -> Option<char>); 7] = [
("\\mathbb{", mathbb_char),
("\\mathcal{", mathcal_char),
("\\mathfrak{", mathfrak_char),
("\\mathsf{", mathsf_char),
("\\mathtt{", mathtt_char),
("\\mathbf{", mathbf_char),
("\\mathit{", mathit_char),
];
while index < text.len() {
let remaining = &text[index..];
let mut matched = false;
for (prefix, mapper) in &alphabets {
if remaining.starts_with(prefix) {
let arg_start = index + prefix.len();
if let Some((arg, next_index)) =
crate::utils::extract_braced(text, arg_start - 1)
{
let formatted = Self::format(&arg);
for ch in formatted.chars() {
if let Some(mapped) = mapper(ch) {
result.push(mapped);
} else {
result.push(ch);
}
}
index = next_index;
matched = true;
break;
}
}
}
if matched {
continue;
}
let ch = remaining.chars().next().unwrap();
result.push(ch);
index += ch.len_utf8();
}
result
}
fn format_accents(text: &str) -> String {
let mut result = String::new();
let mut index = 0;
let accents = [
("\\vec{", "\u{20d7}"), ("\\overrightarrow{", "\u{20d7}"), ("\\overleftarrow{", "\u{20d6}"), ("\\hat{", "\u{0302}"), ("\\widehat{", "\u{0302}"), ("\\tilde{", "\u{0303}"), ("\\widetilde{", "\u{0303}"), ("\\bar{", "\u{0304}"), ("\\overline{", "\u{0305}"), ("\\underline{", "\u{0332}"), ("\\dot{", "\u{0307}"), ("\\ddot{", "\u{0308}"), ("\\dddot{", "\u{20db}"), ("\\ddddot{", "\u{20dc}"), ("\\check{", "\u{030c}"), ("\\breve{", "\u{0306}"), ("\\acute{", "\u{0301}"), ("\\grave{", "\u{0300}"), ("\\mathring{", "\u{030a}"), ];
while index < text.len() {
let remaining = &text[index..];
let mut matched = false;
for (prefix, combining) in &accents {
if remaining.starts_with(prefix) {
let arg_start = index + prefix.len();
if let Some((arg, next_index)) =
crate::utils::extract_braced(text, arg_start - 1)
{
let formatted = Self::format(&arg);
for ch in formatted.chars() {
result.push(ch);
result.push_str(combining);
}
index = next_index;
matched = true;
break;
}
}
}
if matched {
continue;
}
let ch = remaining.chars().next().unwrap();
result.push(ch);
index += ch.len_utf8();
}
result
}
fn format_superscripts(text: &str) -> String {
Self::format_script(text, '^', true)
}
fn format_subscripts(text: &str) -> String {
Self::format_script(text, '_', false)
}
fn format_script(text: &str, marker: char, is_super: bool) -> String {
let mut result = String::new();
let mut index = 0;
while index < text.len() {
let remaining = &text[index..];
let ch = remaining
.chars()
.next()
.expect("remaining is never empty while index < text.len()");
if ch == marker
&& let Some((token, next_index)) =
Self::read_script_token(text, index + ch.len_utf8())
{
result.push_str(&Self::render_script_token(&token, is_super));
index = next_index;
continue;
}
result.push(ch);
index += ch.len_utf8();
}
result
}
fn render_script_token(token: &str, is_super: bool) -> String {
let normalized = crate::math::symbols::replace_math_symbols(token);
if normalized.is_empty() {
return String::new();
}
if normalized.chars().count() == 1 {
let ch = normalized.chars().next().unwrap();
if is_super {
if crate::math::scripts::to_superscript(ch).is_none() {
return format!("^{}", normalized);
}
} else if crate::math::scripts::to_subscript(ch).is_none() {
return format!("_{}", normalized);
}
}
let mut mapped = String::new();
for ch in normalized.chars() {
let mapped_char = if is_super {
crate::math::scripts::to_superscript(ch)
} else {
crate::math::scripts::to_subscript(ch)
};
if let Some(script_char) = mapped_char {
mapped.push(script_char);
} else {
return if is_super {
format!("^({normalized})")
} else {
format!("_({normalized})")
};
}
}
mapped
}
fn read_script_token(text: &str, start: usize) -> Option<(String, usize)> {
if start >= text.len() {
return None;
}
let first = text[start..].chars().next()?;
if first == '{' {
return crate::utils::extract_braced(text, start);
}
if first == '\\' {
let mut index = start + first.len_utf8();
while index < text.len() {
let ch = text[index..].chars().next()?;
if ch.is_alphabetic() || ch == '*' {
index += ch.len_utf8();
} else {
break;
}
}
return Some((text[start..index].to_string(), index));
}
Some((first.to_string(), start + first.len_utf8()))
}
}
fn mathbb_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D538 + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D552 + (ch as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7D8 + (ch as u32 - '0' as u32)),
_ => None,
}
}
fn mathcal_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D49C + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D4B6 + (ch as u32 - 'a' as u32)),
_ => None,
}
}
fn mathfrak_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D504 + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D51E + (ch as u32 - 'a' as u32)),
_ => None,
}
}
fn mathsf_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D5A0 + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D5BA + (ch as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7E2 + (ch as u32 - '0' as u32)),
_ => None,
}
}
fn mathtt_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D670 + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D68A + (ch as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7F6 + (ch as u32 - '0' as u32)),
_ => None,
}
}
fn mathbf_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D400 + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D41A + (ch as u32 - 'a' as u32)),
'0'..='9' => char::from_u32(0x1D7CE + (ch as u32 - '0' as u32)),
_ => None,
}
}
fn mathit_char(ch: char) -> Option<char> {
match ch {
'A'..='Z' => char::from_u32(0x1D434 + (ch as u32 - 'A' as u32)),
'a'..='z' => char::from_u32(0x1D44E + (ch as u32 - 'a' as u32)),
_ => None,
}
}
impl Default for MathFormatter {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::MathFormatter;
#[test]
fn format_pmatrix_uses_parens() {
let formatted = MathFormatter::format_matrices("\\begin{pmatrix}a & b \\\\ c & d\\end{pmatrix}");
assert_eq!(formatted, "(a & b \\\\ c & d)");
}
#[test]
fn format_bmatrix_uses_brackets() {
let formatted = MathFormatter::format_matrices("\\begin{bmatrix}a & b \\\\ c & d\\end{bmatrix}");
assert_eq!(formatted, "[a & b \\\\ c & d]");
}
#[test]
fn format_vmatrix_uses_bars() {
let formatted = MathFormatter::format_matrices("\\begin{vmatrix}a & b \\\\ c & d\\end{vmatrix}");
assert_eq!(formatted, "|a & b \\\\ c & d|");
}
#[test]
fn format_bmatrix_uppercase_uses_braces() {
let formatted = MathFormatter::format_matrices("\\begin{Bmatrix}a \\\\ b\\end{Bmatrix}");
assert_eq!(formatted, "{a \\\\ b}");
}
#[test]
fn format_matrix_plain_no_delimiters() {
let formatted = MathFormatter::format_matrices("\\begin{matrix}a & b \\\\ c & d\\end{matrix}");
assert_eq!(formatted, "a & b \\\\ c & d");
}
#[test]
fn format_vmatrix_double_bars() {
let formatted = MathFormatter::format_matrices("\\begin{Vmatrix}x\\end{Vmatrix}");
assert_eq!(formatted, "\u{2016}x\u{2016}");
}
#[test]
fn format_nested_pmatrix_finds_matching_end() {
let formatted =
MathFormatter::format_matrices("\\begin{pmatrix}a \\begin{pmatrix}x\\end{pmatrix} b\\end{pmatrix}");
assert_eq!(formatted, "(a (x) b)");
}
#[test]
fn format_fraction_with_groups() {
let formatted = MathFormatter::format("\\frac{a+b}{c-d}");
assert_eq!(formatted, "[a+b] รท [c-d]");
}
#[test]
fn format_simple_fractions_as_unicode() {
assert_eq!(MathFormatter::format("\\frac{1}{2}"), "ยฝ");
assert_eq!(MathFormatter::format("\\frac{1}{4}"), "ยผ");
assert_eq!(MathFormatter::format("\\frac{3}{4}"), "ยพ");
assert_eq!(MathFormatter::format("\\frac{1}{3}"), "โ
");
assert_eq!(MathFormatter::format("\\frac{2}{3}"), "โ
");
}
#[test]
fn format_root_and_exponents() {
assert_eq!(MathFormatter::format("\\sqrt{x}"), "โx");
assert_eq!(MathFormatter::format("\\sqrt{b^2}"), "โbยฒ");
assert_eq!(MathFormatter::format("\\sqrt{b^2-4ac}"), "โ(bยฒ-4ac)");
assert_eq!(MathFormatter::format("\\sqrt[3]{8}"), "โ8");
assert_eq!(MathFormatter::format("\\sqrt[4]{16}"), "โ16");
assert_eq!(MathFormatter::format("\\sqrt[5]{32}"), "โตโ32");
}
#[test]
fn format_subscript_and_greek() {
let formatted = MathFormatter::format("x_{i+1}=\\alpha");
assert_eq!(formatted, "xแตขโโ=ฮฑ");
}
#[test]
fn format_unmappable_script_falls_back_to_parenthesized_form() {
let formatted = MathFormatter::format("x^\\infty");
assert_eq!(formatted, "x^โ"); }
#[test]
fn format_vec_accent() {
let formatted = MathFormatter::format("\\vec{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{20d7}'));
}
#[test]
fn format_hat_accent() {
let formatted = MathFormatter::format("\\hat{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0302}'));
}
#[test]
fn format_tilde_accent() {
let formatted = MathFormatter::format("\\tilde{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0303}'));
}
#[test]
fn format_bar_accent() {
let formatted = MathFormatter::format("\\bar{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0304}'));
}
#[test]
fn format_dot_accent() {
let formatted = MathFormatter::format("\\dot{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0307}'));
}
#[test]
fn format_ddot_accent() {
let formatted = MathFormatter::format("\\ddot{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0308}'));
}
#[test]
fn format_mathbb() {
let formatted = MathFormatter::format("\\mathbb{R}");
assert!(formatted.contains('\u{1d549}')); }
#[test]
fn format_mathcal() {
let formatted = MathFormatter::format("\\mathcal{L}");
assert!(formatted.contains('\u{1d4a7}')); }
#[test]
fn format_mathfrak() {
let formatted = MathFormatter::format("\\mathfrak{g}");
assert!(formatted.contains('\u{1d524}')); }
#[test]
fn format_mathbf() {
let formatted = MathFormatter::format("\\mathbf{F}");
assert!(formatted.contains('\u{1d405}')); }
#[test]
fn format_mathit() {
let formatted = MathFormatter::format("\\mathit{x}");
assert!(formatted.contains('\u{1d465}')); }
#[test]
fn format_mathsf() {
let formatted = MathFormatter::format("\\mathsf{x}");
assert!(formatted.contains('\u{1d5d1}')); }
#[test]
fn format_mathtt() {
let formatted = MathFormatter::format("\\mathtt{x}");
assert!(formatted.contains('\u{1d6a1}')); }
#[test]
fn format_binom() {
let formatted = MathFormatter::format("\\binom{n}{k}");
assert!(formatted.contains('n'));
assert!(formatted.contains('k'));
}
#[test]
fn format_tfrac() {
let formatted = MathFormatter::format("\\tfrac{1}{2}");
assert_eq!(formatted, "ยฝ");
}
#[test]
fn format_dfrac() {
let formatted = MathFormatter::format("\\dfrac{a}{b}");
assert!(formatted.contains('a'));
assert!(formatted.contains('b'));
}
#[test]
fn format_nested_sqrt() {
let formatted = MathFormatter::format("\\sqrt{\\sqrt{x}}");
assert!(formatted.contains('โ'));
assert!(formatted.contains('x'));
}
#[test]
fn format_combined_accents_and_greek() {
let formatted = MathFormatter::format("\\vec{\\alpha} + \\hat{\\beta}");
assert!(formatted.contains('ฮฑ'));
assert!(formatted.contains('ฮฒ'));
assert!(formatted.contains('\u{20d7}')); assert!(formatted.contains('\u{0302}')); }
#[test]
fn format_multiple_symbols_in_expression() {
let formatted = MathFormatter::format("\\alpha + \\beta = \\gamma");
assert_eq!(formatted, "ฮฑ + ฮฒ = ฮณ");
}
#[test]
fn format_math_alphabet_full_word() {
let formatted = MathFormatter::format("\\mathbb{N}");
assert!(formatted.contains('\u{1d545}')); }
#[test]
fn format_empty_expression() {
assert_eq!(MathFormatter::format(""), "");
}
#[test]
fn format_plain_text_passthrough() {
assert_eq!(MathFormatter::format("hello world"), "hello world");
}
#[test]
fn format_check_accent() {
let formatted = MathFormatter::format("\\check{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{030c}')); }
#[test]
fn format_breve_accent() {
let formatted = MathFormatter::format("\\breve{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0306}')); }
#[test]
fn format_acute_accent() {
let formatted = MathFormatter::format("\\acute{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0301}')); }
#[test]
fn format_grave_accent() {
let formatted = MathFormatter::format("\\grave{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{0300}')); }
#[test]
fn format_mathring_accent() {
let formatted = MathFormatter::format("\\mathring{x}");
assert!(formatted.contains('x'));
assert!(formatted.contains('\u{030a}')); }
#[test]
fn format_widehat_accent() {
let formatted = MathFormatter::format("\\widehat{AB}");
assert!(formatted.contains('\u{0302}')); }
#[test]
fn format_widetilde_accent() {
let formatted = MathFormatter::format("\\widetilde{AB}");
assert!(formatted.contains('\u{0303}')); }
#[test]
fn format_overrightarrow_accent() {
let formatted = MathFormatter::format("\\overrightarrow{AB}");
assert!(formatted.contains('\u{20d7}')); }
#[test]
fn format_overleftarrow_accent() {
let formatted = MathFormatter::format("\\overleftarrow{AB}");
assert!(formatted.contains('\u{20d6}')); }
#[test]
fn format_overline_accent() {
let formatted = MathFormatter::format("\\overline{x}");
assert!(formatted.contains('\u{0305}')); }
#[test]
fn format_underline_accent() {
let formatted = MathFormatter::format("\\underline{x}");
assert!(formatted.contains('\u{0332}')); }
#[test]
fn format_boldsymbol() {
let formatted = MathFormatter::format("\\boldsymbol{x}");
assert!(formatted.contains('x'));
}
#[test]
fn format_operatorname() {
let formatted = MathFormatter::format("\\operatorname{Tr}");
assert!(formatted.contains("Tr"));
}
#[test]
fn format_displaystyle_noop() {
let formatted = MathFormatter::format("\\displaystyle x^2");
assert!(formatted.contains('x'));
assert!(!formatted.contains("displaystyle"));
}
#[test]
fn format_over_converts_to_frac() {
let over = MathFormatter::format("{a \\over b}");
let frac = MathFormatter::format("\\frac{a}{b}");
assert_eq!(over, frac, "{{a \\over b}} should convert to \\frac{{a}}{{b}}");
}
#[test]
fn format_choose_converts_to_binom() {
let choose = MathFormatter::format("{n \\choose k}");
let binom = MathFormatter::format("\\binom{n}{k}");
assert_eq!(choose, binom, "{{n \\choose k}} should convert to \\binom{{n}}{{k}}");
}
#[test]
fn format_over_does_not_match_overline() {
let formatted = MathFormatter::format("\\overline{x}");
assert!(formatted.contains('\u{0305}'), "overline combining char");
assert!(!formatted.contains("รท"), "should not be treated as a fraction");
}
}