Skip to content

Commit dfaeba9

Browse files
committed
fix: fixed #2892
1 parent fb5ea44 commit dfaeba9

File tree

6 files changed

+42
-30
lines changed

6 files changed

+42
-30
lines changed

CHANGELOG.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,24 @@
8282
arrays with column separators like
8383
`\begin{array}{l|l} a & b\\ c & d\end{array}`.
8484
- **#2515** Fixed placeholders inside accent commands (`\vec{}`, `\bar{}`,
85-
`\hat{}`, etc.) not being clickable. When clicking on a placeholder wrapped
86-
in an accent command, the cursor now correctly positions inside the
87-
placeholder instead of selecting the entire accent atom.
85+
`\hat{}`, etc.) not being clickable. When clicking on a placeholder wrapped in
86+
an accent command, the cursor now correctly positions inside the placeholder
87+
instead of selecting the entire accent atom.
8888
- **#2521** Fixed the `/` shortcut misplacing subscripts when converting a
8989
selection into a fraction. Previously, selecting a symbol with an attached
9090
subscript, e.g. `d_0`, and pressing `/` would move only the base `d` into the
91-
numerator and leave the orphaned `_0` attached to the denominator
92-
placeholder. Selection normalization now automatically expands to include the
93-
associated `subsup` atom so the scripted symbol stays intact inside the
94-
resulting fraction.
91+
numerator and leave the orphaned `_0` attached to the denominator placeholder.
92+
Selection normalization now automatically expands to include the associated
93+
`subsup` atom so the scripted symbol stays intact inside the resulting
94+
fraction.
9595
- **#2558** Fixed cursor jumping to the beginning when entering a left
9696
parenthesis before a previously entered right parenthesis. When typing a
97-
closing bracket first (e.g., `1+2+3)`), then moving the cursor back and
98-
typing an opening bracket `(`, the smart fence feature would correctly create
97+
closing bracket first (e.g., `1+2+3)`), then moving the cursor back and typing
98+
an opening bracket `(`, the smart fence feature would correctly create
9999
`\left(2+3\right)` but would incorrectly position the cursor at the start of
100100
the expression instead of after the opening parenthesis. The cursor now
101-
properly stays positioned inside the newly created fence, right after the
102-
left delimiter.
101+
properly stays positioned inside the newly created fence, right after the left
102+
delimiter.
103103
- **#2547** Fixed rendering of `\colorbox` in fractions where the colored
104104
background would obscure the fraction bar. The background is now rendered
105105
behind the content using a CSS pseudo-element with `z-index: -1`, preventing
@@ -209,6 +209,14 @@
209209
text instead of the styled content itself. The selection boundary logic now
210210
properly checks the next atom's style before advancing, preventing off-by-one
211211
errors when determining style boundaries.
212+
- **#2892** Fixed superscripts and subscripts shifting upward when inside a
213+
`\colorbox` command. Elements with background colors now receive proper
214+
vertical alignment (`vertical-align: -depth`) to prevent positioning issues
215+
with content of different heights. The fix also ensures background colors use
216+
the correct color palette (background colors instead of foreground colors) and
217+
maintains editability via the background color menu. Selection highlighting no
218+
longer causes subscripts to shift, as vertical alignment is only applied to
219+
intentional background colors, not selection highlights.
212220

213221
## 0.107.1 _2025-09-30_
214222

src/atoms/box.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class BoxAtom extends Atom {
106106
if (this.backgroundcolor) {
107107
box.setStyle(
108108
'background-color',
109-
context.toColor(this.backgroundcolor) ?? 'transparent'
109+
context.toBackgroundColor(this.backgroundcolor) ?? 'transparent'
110110
);
111111
}
112112
if (this.framecolor) {

src/core/box.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ export class Box implements BoxInterface {
325325
const color = context.color;
326326
if (color && color !== parent.color) this.setStyle('color', color);
327327

328-
let backgroundColor = context.backgroundColor;
328+
const originalBackgroundColor = context.backgroundColor;
329+
let backgroundColor = originalBackgroundColor;
329330
if (this.isSelected) backgroundColor = highlight(backgroundColor);
330331

331332
if (backgroundColor && backgroundColor !== parent.backgroundColor) {
@@ -336,6 +337,11 @@ export class Box implements BoxInterface {
336337
this.setStyle('--bg-color' as any, backgroundColor);
337338
this.setStyle('display', 'inline-block');
338339
this.setStyle('position', 'relative');
340+
// Add vertical-align to prevent superscript/subscript shifting (#2892)
341+
// Only apply when there's an actual background color (not just selection highlight)
342+
// The depth centers the baseline properly for elements with different heights
343+
if (originalBackgroundColor && originalBackgroundColor !== parent.backgroundColor)
344+
this.setStyle('vertical-align', -this.depth, 'em');
339345
// Add a class that CSS can target to render background via pseudo-element
340346
this.classes = this.classes ? `${this.classes} ML__bg` : 'ML__bg';
341347
}

src/latex-commands/styling.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ defineFunction('boxed', '{content:math}', {
130130
}),
131131
});
132132

133-
// Technically, using a BoxAtom is more correct (there is a small margin
134-
// around it). However, just changing the background color makes editing easier
133+
// Apply background color as a style to allow editing via the background menu.
134+
// The Box rendering will add proper positioning to prevent superscript/subscript shifting.
135135
defineFunction('colorbox', '{:value}{:text*}', {
136136
applyStyle: (style, _name, args: [LatexValue | null], context) => {
137137
return {

0 commit comments

Comments
 (0)