Support \cfrac (#1392)

* Support \cfrac

This was an easier PR to write than #135 since KaTeX now has well established methods for pt-to-em conversions and nulldelimiter.

As in #135, this does not support the LaTeX optional argument for numerator justification.  It supports a `\cfrac #1 #2 ` syntax.

* Added screenshotter files

* fix ref to context in genfrac handler
This commit is contained in:
Ron Kok
2018-06-03 13:38:19 -07:00
committed by Kevin Barabash
parent 3d5826c1da
commit 08b0045b92
6 changed files with 26 additions and 3 deletions

View File

@@ -171,6 +171,7 @@ export type ParseNodeTypes = {
|},
"genfrac": {|
type: "genfrac",
continued: boolean,
numer: ParseNode<*>,
denom: ParseNode<*>,
hasBarLine: boolean,

View File

@@ -12,7 +12,7 @@ import * as mml from "../buildMathML";
defineFunction({
type: "genfrac",
names: [
"\\dfrac", "\\frac", "\\tfrac",
"\\cfrac", "\\dfrac", "\\frac", "\\tfrac",
"\\dbinom", "\\binom", "\\tbinom",
"\\\\atopfrac", // cant be entered directly
],
@@ -29,6 +29,7 @@ defineFunction({
let size = "auto";
switch (funcName) {
case "\\cfrac":
case "\\dfrac":
case "\\frac":
case "\\tfrac":
@@ -49,6 +50,7 @@ defineFunction({
}
switch (funcName) {
case "\\cfrac":
case "\\dfrac":
case "\\dbinom":
size = "display";
@@ -61,6 +63,7 @@ defineFunction({
return new ParseNode("genfrac", {
type: "genfrac",
continued: funcName === "\\cfrac",
numer: numer,
denom: denom,
hasBarLine: hasBarLine,
@@ -89,6 +92,15 @@ defineFunction({
newOptions = options.havingStyle(nstyle);
const numerm = html.buildGroup(group.value.numer, newOptions, options);
if (group.value.continued) {
// \cfrac inserts a \strut into the numerator.
// Get \strut dimensions from TeXbook page 353.
const hStrut = 8.5 / options.fontMetrics().ptPerEm;
const dStrut = 3.5 / options.fontMetrics().ptPerEm;
numerm.height = numerm.height < hStrut ? hStrut : numerm.height;
numerm.depth = numerm.depth < dStrut ? dStrut : numerm.depth;
}
newOptions = options.havingStyle(dstyle);
const denomm = html.buildGroup(group.value.denom, newOptions, options);
@@ -198,7 +210,10 @@ defineFunction({
group.value.leftDelim, delimSize, true,
options.havingStyle(style), group.mode, ["mopen"]);
}
if (group.value.rightDelim == null) {
if (group.value.continued) {
rightDelim = buildCommon.makeSpan([]); // zero width for \cfrac
} else if (group.value.rightDelim == null) {
rightDelim = html.makeNullDelimiter(options, ["mclose"]);
} else {
rightDelim = delimiter.customSizedDelim(

View File

@@ -426,6 +426,7 @@ describe("A frac parser", function() {
const expression = "\\frac{x}{y}";
const dfracExpression = "\\dfrac{x}{y}";
const tfracExpression = "\\tfrac{x}{y}";
const cfracExpression = "\\cfrac{x}{y}";
it("should not fail", function() {
expect(expression).toParse();
@@ -457,6 +458,12 @@ describe("A frac parser", function() {
expect(tfracParse.type).toEqual("genfrac");
expect(tfracParse.value.numer).toBeDefined();
expect(tfracParse.value.denom).toBeDefined();
const cfracParse = getParsed(cfracExpression)[0];
expect(cfracParse.type).toEqual("genfrac");
expect(cfracParse.value.numer).toBeDefined();
expect(cfracParse.value.denom).toBeDefined();
});
it("should parse atop", function() {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

View File

@@ -106,7 +106,7 @@ ExtensibleArrows: |
\xrightleftharpoons[ab]{ABC} + \xhookrightarrow[ab]{ABC} \\
\xtwoheadrightarrow{ABC} + \frac{\xrightarrow[ab]{ABC}}{\xrightarrow[ab]{ABC}} + \left\lvert\xrightarrow[ab]{ABC}\right\rvert
\end{array}
FractionTest: \dfrac{a}{b}\frac{a}{b}\tfrac{a}{b}\;-\dfrac12\;1\tfrac12\;{1 \atop 2}
FractionTest: \dfrac{a}{b}\frac{a}{b}\tfrac{a}{b}\;-\dfrac12\;1\tfrac12\;{1 \atop 2}\; \cfrac{1}{1+\cfrac{1}{x}}
Functions: \sin\cos\tan\ln\log
Gathered: |
\begin{gathered}