Skip to content

Fix \texttt{} in display math contexts #84

@lefticus

Description

@lefticus

Problem

Remaining \texttt{} macros in markdown output, primarily in math contexts. Analysis shows:

  • n4950: 23 instances
  • trunk: 39 instances
  • Total: 62 instances

Root Cause

\tcode{} macros preserved in math mode by Pandoc, appearing as \texttt{} in output.

Solution

Extended cpp-math.lua to handle display math contexts:

  1. Display Math Handler: Added elseif elem.mathtype == "DisplayMath" branch

    • Converts \tcode{}/\texttt{} to backticks before Unicode conversion
    • Applies preprocessing (strips \placeholder{}, \ensuremath{}, text macros)
    • Uses all-or-nothing approach: fully convert to Unicode or revert to preprocessed LaTeX
  2. Test Coverage: Added 4 new tests in test_math.py:

    • test_texttt_in_display_math() - Basic display math blocks
    • test_tcode_in_display_math() - XOR operator handling
    • test_multiple_texttt_in_display_math() - Multiple macros
    • test_texttt_in_complex_display_math() - Multi-line \begin{align*}

Results

  • n4950: 23 → 14 instances (39% reduction, 9 fixed)
  • Test suite: 483 → 487 tests, all passing

Remaining Edge Cases (14 instances)

  • Nested \texttt{\textit{}} (containers.md)
  • Escaped special chars \texttt{\} (lex.md)
  • Complex inline math (ranges.md)
  • Escaped underscores (utilities.md)

These harder edge cases require more sophisticated handling and can be addressed in future work.

Files Changed

  • src/cpp_std_converter/filters/cpp-math.lua - Display math handling
  • tests/test_filters/test_math.py - 4 new tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions