Skip to content

Commit 9a82b5c

Browse files
authored
feat: add LaTeX representation for fractions and corresponding tests (#3434)
1 parent 6907614 commit 9a82b5c

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/utils/latex.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export const latexFunctions = {
124124
expm1: `\\left(e${latexOperators.pow}{\${args[0]}}-1\\right)`,
125125
fix: { 1: '\\mathrm{${name}}\\left(${args[0]}\\right)' },
126126
floor: { 1: '\\left\\lfloor${args[0]}\\right\\rfloor' },
127+
fraction: { 2: '\\frac{${args[0]}}{${args[1]}}' },
127128
gcd: '\\gcd\\left(${args}\\right)',
128129
hypot: '\\hypot\\left(${args}\\right)',
129130
log: {

test/unit-tests/type/fraction/function/fraction.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assert'
2-
import math from '../../../../../src/defaultInstance.js'
32
import Fraction from 'fraction.js'
3+
import math from '../../../../../src/defaultInstance.js'
44

55
describe('fraction', function () {
66
it('should create a fraction', function () {
@@ -81,6 +81,13 @@ describe('fraction', function () {
8181
assert.throws(function () { math.fraction(-Infinity) }, /Error: -Infinity cannot be represented as a fraction/)
8282
assert.throws(function () { math.fraction(NaN) }, /Error: NaN cannot be represented as a fraction/)
8383
})
84+
85+
it('should show a Latex Fraction as one over another', function () {
86+
const node1 = math.parse('fraction(1,2)')
87+
assert.strictEqual(node1.toTex(), '\\frac{1}{2}')
88+
const node2 = math.parse('fraction(a,b)')
89+
assert.strictEqual(node2.toTex(), '\\frac{ a}{\\mathrm{b}}')
90+
})
8491
})
8592

8693
function equalFraction (a, b) {

0 commit comments

Comments
 (0)