md2star.preprocessing.math module

LaTeX math handling for the intermediate Markdown.

Authors often wrap an inline LaTeX formula in backticks — either by reflex (\`$x^2$\`) or because they want to mix prose-style identifiers with math (\`quality_threshold $\in [0,1]$\`). Pandoc treats backtick content as a verbatim code span, so the math is rendered as monospace literal text instead of as math.

This module rewrites those code spans into proper Pandoc math:

  • A code span whose content is purely math is unwrapped: \`$x^2$\`$x^2$.

  • A code span that mixes plain text and math is merged into a single math expression: \`quality_threshold $\in [0,1]$\`$\text{quality threshold} \in [0,1]$. Underscores in the text portion are read as snake_case word separators and become spaces, since a literal _ would mean subscript inside the resulting math context.

A code span with no math (just code/identifiers) is left untouched.

The module also exposes MATH_FORMULA_RE so other passes (e.g. the pipe-table soft-break inserter) can tokenize around math chunks and avoid mutating them.

Author

[Warith HARCHAOUI](https://linkedin.com/in/warith-harchaoui/)

md2star.preprocessing.math.unwrap_math_in_code_spans(text)[source]

Rewrite backtick code spans whose content contains LaTeX math.

Pure-math spans are unwrapped; mixed text/math spans are merged into a single math expression with text portions wrapped in \text{}. Code spans with no math are returned verbatim.

Parameters:

text (str)

Return type:

str