md2notion_api_version icon indicating copy to clipboard operation
md2notion_api_version copied to clipboard

Fix: LaTeX equations being misrendered

Open jeongHwarr opened this issue 8 months ago • 1 comments

Problem

When uploading Markdown files containing LaTeX math expressions to Notion via NotionPyRenderer, expressions like the one below are incorrectly rendered:

  - expressions: $$\hat{\boldsymbol{x}}_{\sigma_{t}}=\hat{\boldsymbol{x}}_{\sigma_{t}} \odot(1-\mathbf{m})+\boldsymbol{x}_{\sigma_{t}} \odot \mathbf{m}$$

In particular, underscores (_) are misinterpreted as emphasis markers (*) due to the following renderer method:

def render_emphasis(self, token):
    return self.renderMultipleToStringAndCombine(token.children, lambda s: f"*{s}*")

Notion result:

\hat{\boldsymbol{x}}*{\sigma*{t}}=\hat{\boldsymbol{x}}*{\sigma*{t}} \odot(1-\mathbf{m})+\boldsymbol{x}*{\sigma*{t}} \odot \mathbf{m}

This causes math symbols to be transformed unintentionally.

Solution

To address this, LaTeX equations (both block and inline using $$) are:

  • Extracted and temporarily replaced with unique placeholders before rendering.
  • Rendered using NotionPyRenderer.
  • Restored into the final Notion-compatible structure using the original math expressions.

This ensures math expressions are rendered correctly and safely in Notion without being altered by the Markdown emphasis rules.

Changes

  • Added extract_equations() to parse and replace math blocks with placeholders
  • Added restore_equations_in_rendered() to restore math expressions after rendering
  • Updated read_file() flow to apply extraction → rendering → restoration
  • Modified Document class to better tokenize math blocks using $$ line markers

jeongHwarr avatar Apr 13 '25 11:04 jeongHwarr

Related issue: https://github.com/veya2ztn/md2notion_api_version/issues/5

jeongHwarr avatar Apr 13 '25 11:04 jeongHwarr