Skip to content

Commit e5ac936

Browse files
committed
Add some more cases where parentheses are needed in text output.
Fractions need parentheses around the numerator or denominator if anything is after a factorial or a SupSub. For example, 2/(3!4!) or 2/(2^2 3^2). SupSubs need parentheses on the superscripts or subscripts if anything is after a factorial. For example, 2^(2!3!) Also, a live fraction should be allowed to be typed after a factorial. For example, if 2! is typed and then the forward slash is typed it should result in the fraction with 2! in the numerator and the cursor in the denominator. Currenlty the 2! is in front of the fraction and the cursor is in the numerator. This fixes openwebwork/webwork2#2821.
1 parent 629bef0 commit e5ac936

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/commands/math/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ const FractionChooseCreateLeftOfMixin = <TBase extends Constructor<MathCommand>>
305305
while (
306306
leftward &&
307307
!(
308-
leftward instanceof BinaryOperator ||
308+
(leftward instanceof BinaryOperator && !(leftward instanceof LatexCmds.factorial)) ||
309309
('text' in LatexCmds && leftward instanceof LatexCmds.text) ||
310310
leftward instanceof UpperLowerLimitCommand ||
311311
leftward.ctrlSeq === '\\ ' ||

src/commands/mathElements.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,13 @@ export class Fraction extends MathCommand {
716716
let numBlocks = 0;
717717
let haveDigits = false;
718718
let numPeriods = 0;
719+
let newBlock = false;
719720
this.ends[dir]?.eachChild((child: TNode) => {
720721
if (child instanceof Digit) haveDigits = true;
721722
if (child instanceof VanillaSymbol && child.ctrlSeq === '.') ++numPeriods;
722723

723724
if (
725+
newBlock ||
724726
!(
725727
child instanceof Digit ||
726728
(child instanceof VanillaSymbol && child.ctrlSeq === '.' && numPeriods < 2 && !numBlocks) ||
@@ -730,6 +732,11 @@ export class Fraction extends MathCommand {
730732
)
731733
++numBlocks;
732734

735+
// These are things that terminate a block. Anything typed after them
736+
// starts a new block and increments the block count above.
737+
newBlock =
738+
('factorial' in LatexCmds && child instanceof LatexCmds.factorial) || child instanceof SupSub;
739+
733740
if (
734741
(haveDigits && numBlocks) ||
735742
numBlocks > 1 ||
@@ -838,11 +845,13 @@ export const supSubText = (prefix: string, block?: TNode) => {
838845
let numBlocks = 0;
839846
let haveDigits = false;
840847
let numPeriods = 0;
848+
let newBlock = false;
841849
block?.eachChild((child: TNode) => {
842850
if (child instanceof Digit) haveDigits = true;
843851
if (child instanceof VanillaSymbol && child.ctrlSeq === '.') ++numPeriods;
844852

845853
if (
854+
newBlock ||
846855
!(
847856
child instanceof Digit ||
848857
(child instanceof VanillaSymbol && child.ctrlSeq === '.' && numPeriods < 2 && !numBlocks) ||
@@ -851,6 +860,10 @@ export const supSubText = (prefix: string, block?: TNode) => {
851860
)
852861
++numBlocks;
853862

863+
// These are things that terminate a block. Anything typed after them
864+
// starts a new block and increments the block count above.
865+
newBlock = 'factorial' in LatexCmds && child instanceof LatexCmds.factorial;
866+
854867
if (
855868
(haveDigits && numBlocks) ||
856869
numBlocks > 1 ||

0 commit comments

Comments
 (0)