fix(muya): keep a wide equation from scrolling under its own tag (#4339) by Jocs · Pull Request #5540 · marktext/marktext · GitHub
Skip to content

fix(muya): keep a wide equation from scrolling under its own tag (#4339) - #5540

Merged
Jocs merged 1 commit into
developfrom
fix/math-tag-overlap-4339
Sep 24, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/math-tag-overlap-4339

Conversation

@Jocs

@Jocs Jocs commented Sep 24, 2026

Copy link
Copy Markdown
Member

Fixes the overlap half of #4339: an equation with \tag{…} that is wider than the editor column is drawn underneath its own tag.

Reproduced

The reporter's formula in a math block, in a column narrower than the formula (a 520px viewport reproduces their screenshot exactly): on develop the tag (A.13) is painted over n^4(V_m − V_K) — 186px of overlap, measured as the last .base's right edge against the tag's left. With this change the overlap is 0 and the tag is reached by scrolling.

The scrollbar half of the report — a horizontal scrollbar under short inline formulas at any Max width percentage — was #5034 and is already fixed by #5466; inline-math-editor-width-5034.spec.ts covers it. (The comment's $\int k dx=kx+C $ no longer parses as math at all: pandoc's tex_math_dollars forbids a space before the closing $, adopted in #5449.)

Mechanism

KaTeX renders a tagged display equation as

<span class="katex-display"><span class="katex"><span class="katex-html">
  <span class="base">…</span>… <span class="tag">(A.13)</span>
</span></span></span>

with .katex-html { display: block; position: relative } and .tag { position: absolute; right: 0 }. .katex-html is a block, so it is as wide as the box around it, never as wide as the formula: the tag is pinned to the column's right edge while the white-space: nowrap formula overflows straight past it. Open upstream as KaTeX/KaTeX#1983.

The fix sizes that box to the formula and puts the tag in the flow:

.katex-display > .katex > .katex-html:has(> .tag) { width: max-content; min-width: 100%; }
.katex-display > .katex > .katex-html > .tag      { position: static; float: right; margin-left: 1em; }
  • overflowing: width: max-content — the float lands after the formula, inside the scrollable extent.
  • fitting: min-width: 100% — the float goes to the column's right margin, the numbered-equation look.

Both properties are needed. Floating the tag alone leaves the box at column width, so the float has nowhere to go and drops to a second line (measured: block height 72 → 112px, overlap 186 → 205px). width: max-content alone keeps the tag absolute, which just moves the overlap from mid-formula to its last 51px.

Scoped with :has(> .tag) so untagged equations — nearly all of them — keep their current box. :has() is already used in this stylesheet. muya never passes leqno/fleqn to KaTeX, so the left-tag variant cannot occur.

Two things this also fixes

  • Same-line display math ($$…$$ inside a paragraph, feat(muya): support same-line display math #4935). Its popup shrinks to fit the formula, so its right edge is the formula's — the tag sat over the tail of even a short formula (23px overlap for a+b=c \tag{1}).
  • The preview floated under an active math block, same structure.

The deliberate cost

A formula that fits is now centred in the space left of its tag, i.e. half the tag's width (~25px for (A.13), ~21px for (1)) left of the box centre. Exact centring and a tag that flows after the formula cannot both hold — this is the trade-off named in the upstream thread — and the overlap is the actual bug. Untagged equations are untouched, which is what the third spec asserts.

Test plan

packages/muya/e2e/tests/blocks/math-tag-overflow-4339.spec.ts, six cases: a block wider than the column, a block that fits, the active block's floating preview, an untagged block, and same-line display math both wider and narrower than the column. Four of the six fail on develop and pass with the rules.

  • pnpm -C packages/muya/e2e exec playwright test --project=chromium tests/blocks/math-tag-overflow-4339.spec.ts — 6 passed; 4 fail with the CSS reverted
  • Every other math spec: inline-math-align, inline-math-editor-width-5034, inline-math-overflow-4339, math-error-nowrap-4100, inline/same-line-display-math, inline/tex-math-dollars — unchanged
  • pnpm -C packages/muya exec stylelint "src/**/*.css" — same 7 findings as on develop, none in the new rules
  • Real app (pnpm run build:unpack, Electron): the rules ship in the bundled renderer CSS; a tagged formula wider than the 700px column scrolls with no overlap, one that fits keeps its tag flush at the right margin
  • typing/math-and-diagrams.spec.ts — one pre-existing local failure ("clicking the rendered inline KaTeX activates the source for editing"), identical with and without this change

🤖 Generated with Claude Code

@Jocs Jocs mentioned this pull request Sep 24, 2026
1 task done
KaTeX positions `\tag` absolutely at the right edge of `.katex-html`, a block
as wide as whatever holds it, so a formula wider than the column was drawn
underneath the tag rather than beside it — the overlap reported in #4339 and
still open upstream as KaTeX/KaTeX#1983.

Size that box to the formula and float the tag instead: the tag then follows
the formula inside the scrollable extent, and `min-width: 100%` keeps it at
the right margin whenever the formula fits, which is the numbered-equation
look. Same-line display math (#4935) is covered by the same rules — its popup
shrinks to fit the formula, so the tag sat over the tail of even a short one.

The one deliberate cost: a formula that fits is now centred in the space left
of its tag, half the tag's width off the box centre. Centring the formula and
flowing the tag after it cannot both hold, and the overlap is the bug.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Jocs
Jocs force-pushed the fix/math-tag-overlap-4339 branch from deeb8b6 to ee5fc57 Compare September 24, 2026 01:44
@Jocs
Jocs merged commit 0c33241 into develop Sep 24, 2026
7 checks passed
@Jocs
Jocs deleted the fix/math-tag-overlap-4339 branch September 24, 2026 01:56
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