LaTeXSwiftUI icon indicating copy to clipboard operation
LaTeXSwiftUI copied to clipboard

[Bug] Single Dollar Sign in Text Breaks LaTeX Rendering

Open resetsix opened this issue 8 months ago • 0 comments

Issue Description

When using LaTeXSwiftUI, a single dollar sign ($) in text content (especially in code blocks) breaks the LaTeX rendering of all subsequent content. This happens because the parser seems to interpret the standalone dollar sign as the beginning of a math expression but doesn't find a matching closing delimiter.

Steps to Reproduce

  1. Create a LaTeX view with content that contains a code block including a dollar sign, followed by actual LaTeX expressions
  2. For example:

Here, \ is used to ensure proper rendering on GitHub.


let content = #"""
\```bash
$ pnpm install
\```

Some text followed by a LaTeX expression: $E=mc^2$
"""#

LaTeX(content)

Current Behavior

The LaTeX content after the standalone dollar sign is not properly rendered. The parser appears to be confused by the single dollar sign in the code block, treating it as the beginning of a math expression.

Expected Behavior

The parser should be smart enough to recognize that a dollar sign within a code block (```bash) or other non-LaTeX contexts should not be treated as a math delimiter. Alternatively, it should provide a mechanism to handle these cases without requiring manual escaping by users.

Workarounds

Currently, we need to manually handle this by:

  1. Removing the dollar sign: pnpm install
  2. Escaping the dollar sign: \$ pnpm install
  3. Adding a matching dollar sign: $ pnpm install $

However, these workarounds require users to modify their content, which is not ideal.

Proposed Solution

The library should be enhanced to:

  1. Ignore dollar signs inside code blocks automatically
  2. Only interpret dollar signs as LaTeX delimiters when they conform to proper LaTeX syntax
  3. Add a configuration option to specify contexts where LaTeX parsing should be disabled

Environment

  • iOS version: iOS 18.1
  • Swift version: 6.0
  • LaTeXSwiftUI version: 1.5.0

Additional Context

This issue is particularly problematic when trying to render Markdown content that contains both code examples with shell commands (which often use $ as a prompt) and mathematical formulas.

Image Image

resetsix avatar Apr 17 '25 02:04 resetsix