Skip to content

fix: mark text runs with \text{} when a text-mode field also holds maths - #3079

Open
blindemann1971 wants to merge 1 commit into
arnog:masterfrom
blindemann1971:fix/text-mode-mixed-serialization
Open

blindemann1971 wants to merge 1 commit into
arnog:masterfrom
blindemann1971:fix/text-mode-mixed-serialization

Conversation

@blindemann1971

Copy link
Copy Markdown

Fixes #3078. Also removes the obstacle behind #2420.

The problem

In a field with defaultMode: "text", text runs serialize bare and maths runs get $ … $:

Hello $x^2$ world   →   "Hello $ x^2 $ world"

That only means what it says to another text-default field. Passed to convertLatexToMarkup(), a server, or a mathfield in maths mode, Hello is five italic variables and its spaces are gone — outside a text-default context, unmarked words are maths.

The two serializers read the same flag and reach opposite conclusions:

file decision
modes-text.ts needsWrap = options.defaultMode !== 'text' → text runs never wrapped in a text-default field
modes-math.ts maths wrapped in $ … $ exactly when options.defaultMode === 'text'

So the maths is marked and the words are not. Fine while the document is all text; ambiguous the moment it is mixed.

The change

Where a run is split by mode, note whether more than one mode is present. If it is, serialize with defaultMode: 'math', which is what both serializers already understand to mean "mark your runs". Words on their own keep today's behaviour, because with nothing to distinguish them from, the wrapper would be noise.

Both call sites that split by mode — the styled path (emitColorRun) and the skipStyles path (Mode.serialize) — now make the same decision, so the two agree.

Result

input (parsed in text mode) before after
Hello $x^2$ world Hello $ x^2 $ world \text{Hello }x^2\text{ world}
Hello $x$ Hello $ x $ \text{Hello }x
$x^2$ alone $ x^2 $ alone x^2\text{ alone}
just words here just words here unchanged
pure maths field x^2+1 unchanged

Tests

  • npm test: 338 passed, 252 snapshots passed — the existing suite is unchanged.
  • Four cases added to test/latex.test.ts; the three mixed-content ones fail on master and pass here, and the fourth pins the "words alone stay unwrapped" behaviour so it cannot regress.
  • Also checked in a browser against a real <math-field default-mode="text">, typing words → Ctrl+M → maths → Ctrl+M → words: mf.value is \text{Hello }x^2\text{ world}, and re-setting that value reproduces the same document.

Note on scope

This changes the LaTeX emitted by text-default fields with mixed content. That is the point — but it is a visible change for anyone who stored the old form. The old form still parses, so stored values keep working; only newly emitted values differ.

In a field with defaultMode "text", a text run is serialized without a
\text{} wrapper and a maths run is wrapped in "$ ... $":

    Hello $x^2$ world   ->   Hello $ x^2 $ world

That value only means what it says to another text-default field. Handed
to anything else that reads LaTeX — convertLatexToMarkup(), renderMathInElement(),
a server, a database — "Hello" is a run of italic variables and its spaces
are gone, because outside a text-default context unmarked words are maths.

A text run that shares the document with maths is now marked, whatever
the field's default mode:

    Hello $x^2$ world   ->   \text{Hello }x^2\text{ world}

Words on their own are untouched: with no maths to tell them apart from,
the whole document is text and the wrapper would be noise.

    just words here     ->   just words here

Both places that split a run by mode make the same decision, so the
styled and skipStyles paths agree.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Text runs are serialized without \text{} when defaultMode is "text", making the value unusable outside the mathfield

1 participant