fix: handle mathml preprocess

This commit is contained in:
liuyuanchuang
2026-02-04 15:52:04 +08:00
parent 56a02eb6da
commit 720cd05add
2 changed files with 264 additions and 6 deletions

View File

@@ -200,8 +200,11 @@ class Converter:
# Extract the LaTeX formula content (remove delimiters)
latex_formula = self._extract_latex_formula(md_text)
# Preprocess formula for better conversion (fix array specifiers, etc.)
preprocessed_formula = self._preprocess_formula_for_conversion(latex_formula)
# Convert to MathML
mathml = self._latex_to_mathml(latex_formula)
mathml = self._latex_to_mathml(preprocessed_formula)
# Convert MathML to mml:math format (with namespace prefix)
mml = self._mathml_to_mml(mathml)
@@ -234,15 +237,16 @@ class Converter:
raise ValueError("LaTeX formula cannot be empty")
# Preprocess formula using the same preprocessing as export
preprocessed = self._preprocess_formula_for_omml(latex_formula.strip())
preprocessed = self._preprocess_formula_for_conversion(latex_formula.strip())
return self._latex_to_omml(preprocessed)
def _preprocess_formula_for_omml(self, latex_formula: str) -> str:
"""Preprocess LaTeX formula for OMML conversion.
def _preprocess_formula_for_conversion(self, latex_formula: str) -> str:
"""Preprocess LaTeX formula for any conversion (MathML, OMML, etc.).
Applies the same preprocessing steps as preprocess_for_export to ensure
consistency. This fixes common issues that cause Pandoc OMML conversion to fail.
consistency across all conversion paths. This fixes common issues that
cause Pandoc conversion to fail.
Args:
latex_formula: Pure LaTeX formula.
@@ -254,7 +258,7 @@ class Converter:
# 1. Convert matrix environments
latex_formula = self._convert_matrix_environments(latex_formula)
# 2. Fix array column specifiers (remove spaces)
# 2. Fix array column specifiers (remove spaces) - THIS IS THE KEY FIX
latex_formula = self._fix_array_column_specifiers(latex_formula)
# 3. Fix brace spacing