Problem in detection of TeX With the $ sign

Hi everyone,

While exploring JupyterLab’s Markdown rendering, I came across issue #19224 regarding smarter handling of inline math delimited by $.

I was able to reproduce behavior that seems related to the issue. In a Markdown cell, text containing currency values can be interpreted as TeX math when enclosed between dollar signs, causing the $ symbols to disappear and the content to be rendered unexpectedly.

For example:

$24 rupees and $23

appears to be treated as an inline math expression rather than plain text containing currency values.

I noticed that the issue mentions Pandoc’s approach of only treating $...$ as math under stricter conditions. I am interested in investigating this further and understanding how JupyterLab currently integrates with MathJax for inline TeX detection.

Could someone point me to the relevant part of the codebase where MathJax/TeX delimiter detection is configured? I would like to explore whether this could be addressed through a custom FindTeX implementation or another extension point.

Thanks!

Hi! I’ve opened a PR that implements this: Add smart inline math dollar parsing — fixes #19224.

How it works: I implemented pandoc’s smart $-delimiter rules, gated behind a new smartInlineMath option (default true):

  • an opening $ is only recognized when the character immediately after it is not whitespace;
  • a closing $ is only recognized when the character immediately before it is not whitespace and the character immediately after it is not a digit.

Where it’s applied:

  1. Markdown math pre-processorremoveMath() in packages/rendermime/src/latex.ts now uses isSmartDollarStart/isSmartDollarEnd when deciding what counts as inline math, so the HTML preview (notebook/cell renderer) treats $24 and $27 as literal text.
  2. MathJax typesetter — a new SmartFindTeX class in packages/mathjax-extension applies the same rules to MathJax’s own FindTeX, so the output document matches. One gotcha worth mentioning: MathJax’s Options.insert() silently drops unknown option keys, so smartInlineMath had to be registered in SmartFindTeX’s static OPTIONS — otherwise the warning MathJax: Invalid option "smartInlineMath" appears and the option is ignored.

The smartInlineMath flag is declared once in ILatexTypesetter.IMathParseOptions (rendermime-interfaces) and shared by both, and it’s part of the MathDocument cache key.

Result: Hello $24 and $27 and $20,000 render literally, while $x^2$, $$...$$, \[...\], and math environments are unchanged.

Tests: added coverage for currency text, digit-after-close rejection ($x^2$1), smartInlineMath: false legacy behavior, and display math — in packages/rendermime/test/latex.spec.ts and packages/mathjax-extension/test/typesetter.spec.ts. Both suites pass (33/33 and 166/167, the latter with one pre-existing skip).