Skip to content

Commit

Permalink
fix: fixed #2124
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Sep 2, 2023
1 parent 533746a commit ac2f460
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
i.e. `(f(x))` would be parsed as `(f(x)`.
- **#2116** Pressing the "/" key after an expression ending with a superscript would
not recognize the left argument as a numerator.
- **#2124** In text mode, some characters were incorrectly interpreted as a math command, for example `(` was interpreted as \lparen`. This could cause some interoperability issues.

### Improvements

Expand Down
2 changes: 0 additions & 2 deletions src/addons/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import {
LATEX_COMMANDS,
MATH_SYMBOLS,
TEXT_SYMBOLS,
ENVIRONMENTS,
} from '../core-definitions/definitions';
import { DEFAULT_KEYBINDINGS } from '../editor/keybindings-definitions';
Expand All @@ -16,7 +15,6 @@ import { getKeybindingMarkup } from '../editor/keybindings';
const MathliveDebug = {
FUNCTIONS: LATEX_COMMANDS,
MATH_SYMBOLS,
TEXT_SYMBOLS,
ENVIRONMENTS,

DEFAULT_KEYBINDINGS,
Expand Down
37 changes: 16 additions & 21 deletions src/core-definitions/definitions-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ const DEFAULT_MACROS: MacroDictionary = {
// Body-text symbols
// See http://ctan.mirrors.hoobly.com/info/symbols/comprehensive/symbols-a4.pdf, p14

export const TEXT_SYMBOLS: Record<string, number> = {
const TEXT_SYMBOLS: Record<string, number> = {
' ': 0x0020,
// want that in Text mode.
'\\!': 0x0021,
Expand All @@ -566,11 +566,13 @@ export const TEXT_SYMBOLS: Record<string, number> = {
'\\%': 0x0025,
'\\&': 0x0026,
'-': 0x002d, // In Math mode, '-' is substituted to U+2212, but we don't
'\\_': 0x005f,
'\\textunderscore': 0x005f, // '_'
'\\euro': 0x20ac,
'\\maltese': 0x2720,
'\\{': 0x007b,
'\\}': 0x007d,
'\\textbraceleft': 0x007b,
'\\textbraceright': 0x007d,
'\\nobreakspace': 0x00a0,
'\\ldots': 0x2026,
'\\textellipsis': 0x2026,
Expand All @@ -583,9 +585,7 @@ export const TEXT_SYMBOLS: Record<string, number> = {
'\\textasciicircum': 0x005e,
'\\textasciitilde': 0x007e,
'\\textasteriskcentered': 0x002a,
'\\textbackslash': 0x005c,
'\\textbraceleft': 0x007b,
'\\textbraceright': 0x007d,
'\\textbackslash': 0x005c, // '\'
'\\textbullet': 0x2022,
'\\textdollar': 0x0024,
'\\textsterling': 0x00a3,
Expand Down Expand Up @@ -634,22 +634,19 @@ if (supportRegexPropertyEscape()) {
*/
function newSymbol(
symbol: string,
value: number | undefined,
codepoint: number | undefined,
type: AtomType = 'mord',
variant?: Variant
): void {
if (value === undefined) return;
if (codepoint === undefined) return;
MATH_SYMBOLS[symbol] = {
definitionType: 'symbol',
type,
variant,
codepoint: value,
codepoint,
};
if (!REVERSE_MATH_SYMBOLS[value]) REVERSE_MATH_SYMBOLS[value] = symbol;

// We accept all math symbols in text mode as well
// which is a bit more permissive than TeX
if (!TEXT_SYMBOLS[symbol]) TEXT_SYMBOLS[symbol] = value;
if (!REVERSE_MATH_SYMBOLS[codepoint])
REVERSE_MATH_SYMBOLS[codepoint] = symbol;
}

/**
Expand Down Expand Up @@ -1081,24 +1078,22 @@ export function unicodeCharToLatex(
* If there is a matching command (e.g. `\alpha`) it is returned.
*/
export function charToLatex(
parseMode: ArgumentType,
parseMode: ParseMode,
codepoint: number | undefined
): string {
if (codepoint === undefined) return '';
if (parseMode === 'math' && REVERSE_MATH_SYMBOLS[codepoint])
return REVERSE_MATH_SYMBOLS[codepoint];

if (parseMode === 'text') {
let textSymbol = Object.keys(TEXT_SYMBOLS).find(
const textSymbol = Object.keys(TEXT_SYMBOLS).find(
(x) => TEXT_SYMBOLS[x] === codepoint
);
if (!textSymbol) {
const hex = codepoint.toString(16);
textSymbol = '^'.repeat(hex.length) + hex;
}

return textSymbol;
if (textSymbol) return textSymbol;
return String.fromCodePoint(codepoint);
}

// const hex = codepoint.toString(16).toLowerCase();
// return '^'.repeat(hex.length) + hex;
return String.fromCodePoint(codepoint);
}
4 changes: 2 additions & 2 deletions src/core-definitions/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ newSymbols([

['\\lbrace', 0x007b, 'mopen'],
['\\rbrace', 0x007d, 'mclose'],
['\\lparen', 0x0028, 'mopen'],
['\\rparen', 0x0029, 'mclose'],
['\\lparen', 0x0028, 'mopen'], // mathtools.sty
['\\rparen', 0x0029, 'mclose'], // mathtools.sty
['\\langle', 0x27e8, 'mopen'],
['\\rangle', 0x27e9, 'mclose'],
['\\lfloor', 0x230a, 'mopen'],
Expand Down
8 changes: 4 additions & 4 deletions src/editor-mathfield/keyboard-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,9 +1021,9 @@ function isValidClose(open: string | undefined, close: string): boolean {
if (!open) return true;

if (
['(', '{', '[', '\\lbrace', '\\lparen', '\\{', '\\lbrack'].includes(open)
['(', '\\lparen', '{', '\\{', '\\lbrace', '[', '\\lbrack'].includes(open)
) {
return [')', '}', ']', '\\rbrace', '\\rparen', '\\}', '\\rbrack'].includes(
return [')', '\\rparen', '}', '\\}', '\\rbrace', ']', '\\rbrack'].includes(
close
);
}
Expand All @@ -1034,9 +1034,9 @@ function isValidOpen(open: string, close: string | undefined): boolean {
if (!close) return true;

if (
[')', '}', ']', '\\rbrace', '\\rparen', '\\}', '\\rbrack'].includes(close)
[')', '\\rparen', '}', '\\}', '\\rbrace', ']', '\\rbrack'].includes(close)
) {
return ['(', '{', '[', '\\lbrace', '\\lparen', '\\{', '\\lbrack'].includes(
return ['(', '\\lparen', '{', '\\{', '\\lbrace', '[', '\\lbrack'].includes(
open
);
}
Expand Down
1 change: 0 additions & 1 deletion src/mathlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export const version = {
// export const debug = {
// FUNCTIONS: MathLiveDebug.FUNCTIONS,
// MATH_SYMBOLS: MathLiveDebug.MATH_SYMBOLS,
// TEXT_SYMBOLS: MathLiveDebug.TEXT_SYMBOLS,
// ENVIRONMENTS: MathLiveDebug.ENVIRONMENTS,
// DEFAULT_KEYBINDINGS: MathLiveDebug.DEFAULT_KEYBINDINGS,
// getKeybindingMarkup: MathLiveDebug.getKeybindingMarkup,
Expand Down

0 comments on commit ac2f460

Please sign in to comment.