feat: Allow text-mode accents in math mode, except in strict mode (#3009)

* feat: Allow text-mode accents in math mode, except in strict mode

LaTeX only issues a warning when using e.g. `\r` in math mode.
KaTeX will now do the same with the default `strict` setting of `warn`.

Fixes #2983

* Fix accent mode

Co-authored-by: ylemkimon <y@ylem.kim>
This commit is contained in:
Erik Demaine
2021-05-14 17:08:39 -04:00
committed by GitHub
parent 85687f992f
commit 0e9acce9be
3 changed files with 21 additions and 8 deletions

View File

@@ -249,15 +249,22 @@ defineFunction({
props: { props: {
numArgs: 1, numArgs: 1,
allowedInText: true, allowedInText: true,
allowedInMath: false, allowedInMath: true, // unless in strict mode
argTypes: ["primitive"], argTypes: ["primitive"],
}, },
handler: (context, args) => { handler: (context, args) => {
const base = args[0]; const base = args[0];
let mode = context.parser.mode;
if (mode === "math") {
context.parser.settings.reportNonstrict("mathVsTextAccents",
`LaTeX's accent ${context.funcName} works only in text mode`);
mode = "text";
}
return { return {
type: "accent", type: "accent",
mode: context.parser.mode, mode: mode,
label: context.funcName, label: context.funcName,
isStretchy: false, isStretchy: false,
isShifty: true, isShifty: true,

View File

@@ -1,3 +1,5 @@
import {strictSettings} from "./helpers";
describe("Parser:", function() { describe("Parser:", function() {
describe("#handleInfixNodes", function() { describe("#handleInfixNodes", function() {
@@ -84,10 +86,14 @@ describe("Parser:", function() {
"Can't use function '\\sqrt' in text mode" + "Can't use function '\\sqrt' in text mode" +
" at position 7: \\text{\\̲s̲q̲r̲t̲2 is irrational…"); " at position 7: \\text{\\̲s̲q̲r̲t̲2 is irrational…");
}); });
it("rejects text-mode-only functions in math mode", function() { it("rejects text-mode-only functions in math mode", () => {
expect`\'echec`.toFailWithParseError( expect`$`.toFailWithParseError(
"Can't use function '\\'' in math mode" + "Can't use function '$' in math mode at position 1: $̲");
" at position 1: \\̲'̲echec"); });
it("rejects strict-mode text-mode-only functions in math mode", () => {
expect`\'echec`.toFailWithParseError("LaTeX-incompatible input " +
"and strict mode is set to 'error': LaTeX's accent \\' works " +
"only in text mode [mathVsTextAccents]", strictSettings);
}); });
}); });

View File

@@ -56,8 +56,8 @@ expect.extend({
return expectKaTeX(expr, settings, Mode.PARSE, this.isNot); return expectKaTeX(expr, settings, Mode.PARSE, this.isNot);
}, },
toFailWithParseError: function(expr, expected = ParseError) { toFailWithParseError: function(expr, expected = ParseError, settings) {
return expectKaTeX(expr, undefined, Mode.PARSE, this.isNot, expected); return expectKaTeX(expr, settings, Mode.PARSE, this.isNot, expected);
}, },
toBuild(expr, settings) { toBuild(expr, settings) {