Skip to content

remend treats LaTeX \(...\) and \[...\] math as text when completing emphasis #522

@PentaTea

Description

@PentaTea

Bug Description

remend recognizes $...$ and $$...$$ as math contexts when deciding whether * or _ should be ignored by emphasis completion. It does not currently recognize LaTeX paren-style inline math (\(...\)) or bracket-style display math (\[...\]).

As a result, emphasis markers inside these math expressions can be counted as Markdown emphasis delimiters. This can cause remend to append a spurious _ or *, or to leave surrounding emphasis incomplete, even though the marker was part of a math expression.

This commonly appears with subscripts such as x_{t+1}, where the _ is not word-internal because it is followed by {.

Steps to Reproduce

import remend from "remend";

const text = [
  String.raw`> Given tokens \(x_1, ..., x_t\), maximize \(P(x_{t+1} | x_1, ..., x_t)\)`,
  "",
  "**2. Instruction Tuning (SFT)**",
  "- Dataset size is much smaller than pre-training",
].join("\n");

// Simulates a streaming snapshot before the heading has fully arrived.
const partial = text.slice(0, text.indexOf("(SFT)") + "(SFT".length);

console.log(remend(partial));

Expected Behavior

> Given tokens \(x_1, ..., x_t\), maximize \(P(x_{t+1} | x_1, ..., x_t)\)

**2. Instruction Tuning (SFT**

The incomplete bold span is completed cleanly, and math subscripts inside \(...\) are ignored by emphasis completion.

Actual Behavior

> Given tokens \(x_1, ..., x_t\), maximize \(P(x_{t+1} | x_1, ..., x_t)\)

**2. Instruction Tuning (SFT**_

A spurious _ is appended because the _ in x_{t+1} is counted as an unclosed single-underscore italic delimiter.

The same issue also affects complete documents:

console.log(remend(String.raw`\(P(x_{t+1})\)

plain trailing text.`));

Expected:

\(P(x_{t+1})\)

plain trailing text.

Actual:

\(P(x_{t+1})\)

plain trailing text._

And bracket-style math has the same behavior:

console.log(remend(String.raw`\[
P(x_{t+1} | x_t)
\]`));

Expected:

\[
P(x_{t+1} | x_t)
\]

Actual:

\[
P(x_{t+1} | x_t)
\]_

Impact

This affects streamed Markdown that contains LaTeX math using \(...\) or \[...\], which is common in model outputs. The visible result is extra Markdown syntax appearing in unrelated text after the math expression.

Root Cause

isWithinMathBlock currently detects $...$ and $$...$$, but not \(...\) or \[...\].

The emphasis handlers rely on this math-context detection to skip * and _ inside math. Since LaTeX paren/bracket math is not recognized as math, markers inside those expressions are treated as normal Markdown emphasis markers.

Environment

  • remend: 1.3.0
  • Observed via streamdown: 2.5.0

Proposed Fix

Recognize LaTeX paren-style and bracket-style math delimiters as math contexts:

  • \( opens inline math
  • \) closes inline math
  • \[ opens display math
  • \] closes display math

The emphasis marker skip logic should also treat these delimiters as a reason to check math context, not only dollar-based math.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions