Skip to content

Glue inline math to hugging punctuation so it can't orphan-wrap#215

Merged
PatrikBak merged 1 commit into
mainfrom
patrik/math-nowrap-glue
Jun 22, 2026
Merged

Glue inline math to hugging punctuation so it can't orphan-wrap#215
PatrikBak merged 1 commit into
mainfrom
patrik/math-nowrap-glue

Conversation

@PatrikBak

Copy link
Copy Markdown
Owner

KaTeX renders inline math inside a display:inline-block box — an atomic inline the browser can soft-wrap on either side. That let a period written right after a formula ($x$.) or a bracket written right before one (($x$) flow onto its own line, away from the formula it belongs to.

This wraps each inline formula together with the whitespace-free run hugging it on either side in a white-space:nowrap span, on both render paths via shared glue primitives (takeLeadingGlue / takeTrailingGlue):

  • String rendererrenderMathContentToHtml rebuilt around a typed-segment model (TextSegment | InlineMathSegment | DisplayMathSegment) plus a glue pass.
  • Markdown pipeline — a new rehype-math-nowrap plugin appended after rehype-katex in both the shared <Markdown> pipeline and the headless validator.

CSS rounds it out: hyphens: none in math prose, a p:has(.katex) carve-out that backs WebKit off text-wrap: pretty (which mishandles the atomic nowrap spans), and a hanging-indent gutter so wrapped list lines align under the text rather than the marker.

Covered by new vitest specs for the glue primitives, both render paths (including escaping, shared-text gluing, and the previously-untested inlineBlockToMathSource), and idempotency.

🤖 Generated with Claude Code

KaTeX renders inline math inside a display:inline-block box — an atomic
inline the browser can soft-wrap on both sides. That let a period written
right after a formula ($x$.) or a bracket written right before one (($x$)
flow onto its own line, away from the formula it belongs to.

Wrap each inline formula together with the whitespace-free run hugging it on
either side in a white-space:nowrap span. This lands on both render paths via
shared glue primitives (takeLeadingGlue/takeTrailingGlue): the string renderer
(renderMathContentToHtml, rebuilt around a typed-segment model) and the
markdown pipeline (a new rehype-math-nowrap plugin run after rehype-katex).

CSS rounds it out: hyphens:none in math prose, a p:has(.katex) carve-out that
backs WebKit off text-wrap:pretty (which mishandles the atomic nowrap spans),
and a hanging-indent gutter so wrapped list lines align under the text.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
math-comps Ready Ready Preview, Comment Jun 22, 2026 12:20pm

@PatrikBak PatrikBak enabled auto-merge June 22, 2026 12:19
@PatrikBak PatrikBak merged commit ec89352 into main Jun 22, 2026
5 checks passed
@PatrikBak PatrikBak deleted the patrik/math-nowrap-glue branch June 22, 2026 12:23
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.

1 participant