Skip to content

Equation pipeline creates tracked changes: TrackRevisions not disabled before macro execution #1

Description

@GLY2024

Problem

When converting MathType OLE equations via the equation pipeline (03run_equation_pipeline.py), both step 2 (OLE → MathML) and step 3 (MathML → OMML) run Word macros without disabling TrackRevisions. This causes the equation conversion to be recorded as tracked changes (deletions of old OLE/MathML + insertions of new MathML/OMML).

When the resulting .docx is subsequently converted to markdown via pandoc --track-changes=all, the deleted MathML/MathType markup appears as raw garbled text in the output — thousands of lines of <math xmlns=...>, <semantics>, MathType@MTEF@5@5@+=... etc.

Reproduction

# 1. Convert a docx with MathType OLE equations
python scripts/math_ops/03run_equation_pipeline.py input.docx --out clean.docx

# 2. Convert to markdown with tracked changes
pandoc --track-changes=all clean.docx -o output.md

# 3. output.md contains raw MathML as deletion spans:
# [**\<math xmlns=\'http://www.w3.org/1998/Math/MathML\'\>**]{.deletion author="..." ...}
# [**\<semantics\>**]{.deletion author="..." ...}
# ... (thousands of lines per equation)

Root Cause

In 01convert_equation_format_OLE_to_MathML.ps1 (line ~263) and 02convert_equation_format_MathML_to_OMML.ps1 (line ~326), $word.Run(...) is called while $doc.TrackRevisions may be $true, so Word records every equation replacement as a tracked change.

Fix

Disable TrackRevisions before macro execution and restore afterward, in both scripts:

01convert_equation_format_OLE_to_MathML.ps1 (before $word.Run):

$savedTrackRevisions = $doc.TrackRevisions
$doc.TrackRevisions = $false

Write-NoticeMessage "开始执行宏:$MacroName"
Write-NoticeMessage "请在 Word 弹窗中手动确认。"
[void]$word.Run($MacroName)

$doc.TrackRevisions = $savedTrackRevisions

02convert_equation_format_MathML_to_OMML.ps1 (before $word.Run):

$savedTrackRevisions = $doc.TrackRevisions
$doc.TrackRevisions = $false

Write-NoticeMessage "正在执行宏:$macroQualifiedName"
try {
    $null = $word.Run($macroQualifiedName)
}
catch {
    $detail = $_.Exception.Message
    throw "执行宏失败:$detail`n`n$((Get-InstallHint -Template $TemplateName).Trim())"
}

$doc.TrackRevisions = $savedTrackRevisions

This preserves all human-authored tracked changes in the document while preventing equation-conversion artifacts.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions