Skip to content

Conversation

@Abhijay007
Copy link
Collaborator

Closes: #6196

PR Summary

This PR fixes an issue with KaTeX math rendering where underscores (_) were incorrectly treated as subscripts. As a result, text such as $FOO_BAR or variable_name was rendered in math mode, making code snippets and shell examples unreadable.

The root cause was that remark-math plugin treats single-dollar expressions ($text$) as inline math by default. When users write shell variables like $FOO_BAR, it unintentionally enters math mode, and KaTeX interprets _ as a subscript operator.

Changes Made

Configuration Fix

  • Set singleDollarTextMath: false in the remarkMath plugin.
  • Single $ is now treated as plain text, preventing accidental math rendering.
  • Users must use $$...$$ for display math.

Files Modified

  • ui/desktop/src/components/MarkdownContent.tsx — added configuration and preprocessing logic.

Trade-off

Note: Users must now use $$...$$ for math instead of $...$.

Why this is acceptable:

  • Goose is primarily a coding assistant, where code and shell commands are more common than math.
  • This matches the behavior of platforms
  • This approach aligns with recommendations from the remark-math maintainers and is adopted by other platforms (e.g., GitLab) facing similar issues.

Type of Change

  • Bug fix

AI Assistance

  • This PR was created or reviewed with AI assistance.

Testing

Tested in the desktop UI with:

  • Plain text containing $...$
  • Math expressions using $$...$$
  • Shell commands and code snippets to ensure underscores render correctly.

Screenshots / Demos (UX)

Before:

beforeKatex

After:

afterKatex

Signed-off-by: Abhijay007 <Abhijay007j@gmail.com>
@Abhijay007 Abhijay007 requested review from Copilot and zanesq and removed request for Copilot December 22, 2025 18:44
@DOsinga
Copy link
Collaborator

DOsinga commented Dec 22, 2025

why does $FOO_BAR enter math mode? shouldn't there be a closing tag $

@Abhijay007
Copy link
Collaborator Author

why does $FOO_BAR enter math mode? shouldn't there be a closing tag $

The thing is with the default remark-math configuration, singleDollarTextMath is enabled (true) by default, which means a single $ is enough to trigger inline math mode. When the parser encounters a $, it starts looking for a closing $.

However, within math mode, the underscore character (_) is interpreted as a LaTeX subscript operator. As a result, a string like $FOO_BAR is parsed as the start of inline math, where FOO_BAR is interpreted as FOO with BAR as a subscript. Even if a closing $ is missing, KaTeX/remark-math still attempts to parse it as incomplete math, which leads to rendering issues.

@DOsinga
Copy link
Collaborator

DOsinga commented Dec 24, 2025

yes, but my concern is that if you run this test:

describe('KaTeX Math Rendering - singleDollarTextMath: false', () => {
it('treats single dollar signs as plain text', async () => {
const content = 'The formula $x_i represents the i-th element.';

  const { container } = render(<MarkdownContent content={content} />);

  await waitFor(() => {
    const katexElements = container.querySelectorAll('.katex');
    expect(katexElements.length).toBe(0);
    expect(container).toHaveTextContent('$x_i');
  });
});
});

on main, it succeeds, so it is not that if it fails to find a closing $ it executes.

@Abhijay007
Copy link
Collaborator Author

yes, but my concern is that if you run this test:

describe('KaTeX Math Rendering - singleDollarTextMath: false', () => { it('treats single dollar signs as plain text', async () => { const content = 'The formula $x_i represents the i-th element.';

  const { container } = render(<MarkdownContent content={content} />);

  await waitFor(() => {
    const katexElements = container.querySelectorAll('.katex');
    expect(katexElements.length).toBe(0);
    expect(container).toHaveTextContent('$x_i');
  });
});
});

on main, it succeeds, so it is not that if it fails to find a closing $ it executes.

Um I tried this test on main, it's failing for me for the same reason that it counts it as a subscript

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.

issues with LaTeX rendering in agent response

3 participants