mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 11:48:41 +00:00
Implements \mathchoice command (#969)
This PR implements `\mathchoice` function. I once created PR on the wrong branch. Sorry for the mess. This is particularly useful when one defines custom macro for "big operators". For example: ```latex \newcommand{\infdisj}{% \mathop{% \mathchoice{% display \bigvee\hspace{-2ex}\bigvee% }{ % inline \bigvee\hspace{-1.75ex}\bigvee% }{ % script \bigvee\hspace{-1.4ex}\bigvee% }{ % scriptscript \bigvee\hspace{-1ex}\bigvee% }}} ```
This commit is contained in:
committed by
Erik Demaine
parent
2d32263998
commit
6f1661f7da
@@ -566,3 +566,6 @@ defineFunction(["\\verb"], {
|
||||
throw new ParseError(
|
||||
"\\verb ended by end of line instead of matching delimiter");
|
||||
});
|
||||
|
||||
// MathChoice
|
||||
import "./functions/mathchoice";
|
||||
|
57
src/functions/mathchoice.js
Normal file
57
src/functions/mathchoice.js
Normal file
@@ -0,0 +1,57 @@
|
||||
// @flow
|
||||
import defineFunction, {ordargument} from "../defineFunction";
|
||||
import buildCommon from "../buildCommon";
|
||||
import mathMLTree from "../mathMLTree";
|
||||
import Style from "../Style";
|
||||
import * as html from "../buildHTML";
|
||||
import * as mml from "../buildMathML";
|
||||
|
||||
const chooseMathStyle = (group, options) => {
|
||||
const style = options.style;
|
||||
if (style.size === Style.DISPLAY.size) {
|
||||
return group.value.display;
|
||||
} else if (style.size === Style.TEXT.size) {
|
||||
return group.value.text;
|
||||
} else if (style.size === Style.SCRIPT.size) {
|
||||
return group.value.script;
|
||||
} else if (style.size === Style.SCRIPTSCRIPT.size) {
|
||||
return group.value.scriptscript;
|
||||
}
|
||||
return group.value.text;
|
||||
|
||||
};
|
||||
|
||||
defineFunction({
|
||||
type: "mathchoice",
|
||||
names: ["\\mathchoice"],
|
||||
props: {
|
||||
numArgs: 4,
|
||||
},
|
||||
handler: (context, args) => {
|
||||
return {
|
||||
type: "mathchoice",
|
||||
display: ordargument(args[0]),
|
||||
text: ordargument(args[1]),
|
||||
script: ordargument(args[2]),
|
||||
scriptscript: ordargument(args[3]),
|
||||
};
|
||||
},
|
||||
htmlBuilder: (group, options) => {
|
||||
const body = chooseMathStyle(group, options);
|
||||
const elements = html.buildExpression(
|
||||
body,
|
||||
options,
|
||||
false
|
||||
);
|
||||
return new buildCommon.makeFragment(elements);
|
||||
},
|
||||
mathmlBuilder: (group, options) => {
|
||||
const body = chooseMathStyle(group, options);
|
||||
const elements = mml.buildExpression(
|
||||
body,
|
||||
options,
|
||||
false
|
||||
);
|
||||
return new mathMLTree.MathNode("mrow", elements);
|
||||
},
|
||||
});
|
Reference in New Issue
Block a user