Repository navigation
fix: mark text runs with \text{} when a text-mode field also holds maths - #3079
Open
blindemann1971 wants to merge 1 commit into
Open
blindemann1971 wants to merge 1 commit into
blindemann1971 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3078. Also removes the obstacle behind #2420.
The problem
In a field with
defaultMode: "text", text runs serialize bare and maths runs get$ … $:That only means what it says to another text-default field. Passed to
convertLatexToMarkup(), a server, or a mathfield in maths mode,Hellois 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:
modes-text.tsneedsWrap = options.defaultMode !== 'text'→ text runs never wrapped in a text-default fieldmodes-math.ts$ … $exactly whenoptions.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 theskipStylespath (Mode.serialize) — now make the same decision, so the two agree.Result
Hello $x^2$ worldHello $ x^2 $ world\text{Hello }x^2\text{ world}Hello $x$Hello $ x $\text{Hello }x$x^2$ alone$ x^2 $ alonex^2\text{ alone}just words herejust words herex^2+1Tests
npm test: 338 passed, 252 snapshots passed — the existing suite is unchanged.test/latex.test.ts; the three mixed-content ones fail onmasterand pass here, and the fourth pins the "words alone stay unwrapped" behaviour so it cannot regress.<math-field default-mode="text">, typing words → Ctrl+M → maths → Ctrl+M → words:mf.valueis\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.