Skip to content

fix: handle escaped dollar signs in inline math#438

Open
saschabuehrle wants to merge 1 commit intolepture:mainfrom
saschabuehrle:fix/issue-370
Open

fix: handle escaped dollar signs in inline math#438
saschabuehrle wants to merge 1 commit intolepture:mainfrom
saschabuehrle:fix/issue-370

Conversation

@saschabuehrle
Copy link
Copy Markdown

Bug

Issue #370 — Escaped $ in inline math expressions are incorrectly parsed, causing the math expression to be split at the escaped dollar sign.

Problem

The current inline math pattern r"$(?!\s)(?P<math_text>.+?)(?!\s)$" uses a simple non-greedy match that doesn't handle escaped characters. For input like $x=\$$, it incorrectly parses as:

  • Math content: x=\
  • Text content: $

Solution

Updated the pattern to r"$(?!\s)(?P<math_text>(?:[^$\\]|\\.)+?)(?!\s)$" which:

  • Matches non-dollar, non-backslash characters: [^$\\]
  • Matches escaped sequences: \\.
  • Ensures escaped \$ characters are treated as literal content within math expressions

Testing

  • Added test case for escaped dollar signs in math expressions
  • All existing math tests continue to pass
  • Verified the fix handles $x=\$$ correctly as a single math expression x=\$

Greetings, saschabuehrle

The inline math pattern now correctly handles escaped $ characters
within math expressions. Previously, $x=$$ would incorrectly parse
as math 'x=\' followed by text '$', but now correctly parses as a
single math expression 'x=$'.

The fix uses a more precise regex pattern that matches either:
- Non-dollar, non-backslash characters: [^$\]
- Escaped sequences: \.

This ensures that escaped $ characters are treated as literal content
within the math expression rather than as delimiters.
@sonarqubecloud
Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant