mirror of
https://github.com/run-llama/chat-ui.git
synced 2026-07-21 11:25:22 -04:00
[PR #70] [MERGED] fix: properly convert LaTeX delimiters and prevent $ parsing issues #97
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/run-llama/chat-ui/pull/70
Author: @loharvikas
Created: 4/12/2025
Status: ✅ Merged
Merged: 4/18/2025
Merged by: @thucpn
Base:
main← Head:main📝 Commits (3)
81e59d8fix: properly convert LaTeX delimiters and prevent $ parsing issues95de05afix: properly convert LaTeX delimiters and prevent $ parsing issuesf9a1808Mino change📊 Changes
2 files changed (+9 additions, -3 deletions)
View changed files
📝
apps/web/app/simple-demo.tsx(+1 -1)📝
packages/chat-ui/src/widgets/markdown.tsx(+8 -2)📄 Description
🛠️ Fix: Improve LaTeX Preprocessing Logic
Problem 1: Incorrect Regex for Inline Math
The existing logic incorrectly attempts to replace inline LaTeX delimiters using the wrong pattern:
This matches
\[...\], which is block-level syntax, not inline.Inline math in LaTeX is denoted with
\\(...\\), so this pattern was failing to process inline math correctly.Problem 2: Risky Use of
$as a Math DelimiterThe current implementation converts LaTeX delimiters to
$...$and$$...$$, which can conflict with regular content that includes dollar signs:Example:
This could incorrectly treat
10 and the discount isas a LaTeX expression.✅ Solution
$signs in the content by replacing them with\$\\[...\\]➜$$...$$(block-level)\\(...\\)➜$...$(inline)Updated logic:
🧪 Demo Update
simple-demo.tsxUpdated the demo content to use standard LaTeX delimiters (
\\[...\\]and\\(...\\)), instead of embedding raw$signs:Before:
After:
This ensures the input content matches how most LLMs output LaTeX—using
\(...\)for inline and\[...\]for block-level math.📚 Why Avoid
$...$for Math?Why Avoid
$...$for Inline Math?While
$$...$$works fine for block-level math, using$...$for inline math is not recommended.Dollar signs frequently appear in regular text—especially in financial or casual contexts—so
$...$can lead to incorrect parsing. For example, a sentence like:"We raised $20 million and $10 million..."
might render incorrectly as:
"We raised 20millionand10 million..."
To avoid issues like this, it's better to use
\(...\)for inline math and\[...\]for block math. This approach allows us to safely convert to dollar-based delimiters later, if needed, while avoiding ambiguity and parsing errors.Ref: https://docs.mathjax.org/en/latest/input/tex/delimiters.html
Problem 1: Improper regex for handling inline math ( )
E.g:
\( x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \)Before
After fix
Problem 2: Rendering issue when $ is used in non math content.
E.g
$20 million and $10 millionBefore
After fix
Verified math content renders properly with new changes
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.