Skip to content

Commit 513d479

Browse files
author
David Kutugata
authored
Fix latex manipulation case with multiple formulas inside a single '$$' block (#9971)
* Changed the latex manipulation to only fix single formulas. * added news file * Fixed an issue with multiple latex formulas in the same '$$' block. * reverted changes on old tests
1 parent 0f65671 commit 513d479

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

news/2 Fixes/9766.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an issue with multiple latex formulas in the same '$$' block.

src/datascience-ui/interactive-common/latexManipulation.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function fixLatexEquations(input: string): string {
3131
// Pick the first that matches
3232
let match = dollars;
3333
let isBeginMatch = false;
34+
const isDollarsMatch = dollars?.index === dollar?.index;
3435
if (!match || (dollar && dollar.index < match.index)) {
3536
match = dollar;
3637
endRegex = /\$/;
@@ -58,8 +59,20 @@ export function fixLatexEquations(input: string): string {
5859
// Invalid, just return
5960
return input;
6061
}
62+
} else if (isDollarsMatch) {
63+
// Output till the next $$
64+
const offset = match.index + 2 + start;
65+
const endDollar = endRegex.exec(input.substr(offset));
66+
if (endDollar) {
67+
const length = endDollar.index + 2 + offset;
68+
output.push(input.substr(start, length));
69+
start = start + length;
70+
} else {
71+
// Invalid, just return
72+
return input;
73+
}
6174
} else {
62-
// Output till the next $ or $$
75+
// Output till the next $
6376
const offset = match.index + 1 + start;
6477
const endDollar = endRegex.exec(input.substr(offset));
6578
if (endDollar) {

src/test/datascience/latexManipulation.unit.test.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ $$
8989
$$
9090
\\begin{equation*}
9191
\\mathbf{V}_1 \\times \\mathbf{V}_2 = \\begin{vmatrix}
92-
\\mathbf{i} & \\mathbf{j} & \\mathbf{k} \\
93-
\\frac{\partial X}{\\partial u} & \\frac{\\partial Y}{\\partial u} & 0 \\\\
94-
\\frac{\partial X}{\\partial v} & \\frac{\\partial Y}{\\partial v} & 0
92+
\\mathbf{i} & \\mathbf{j} & \\mathbf{k} \\\\
93+
\\frac{\\partial X}{\\partial u} & \\frac{\\partial Y}{\\partial u} & 0 \\\\
94+
\\frac{\\partial X}{\\partial v} & \\frac{\\partial Y}{\\partial v} & 0
9595
\\end{vmatrix}
9696
\\end{equation*}
9797
$$
@@ -116,6 +116,42 @@ $$
116116
This expression $\\sqrt{3x-1}+(1+x)^2$ is an example of a TeX inline equation in a [Markdown-formatted](https://daringfireball.net/projects/markdown/) sentence.
117117
`;
118118

119+
const markdown6 = `$$
120+
\\begin{aligned}
121+
\\frac{\\partial}{\\partial\\omega_j}C(\\omega) &= \\frac1m\\sum_{i=1}^m\\varphi_j\\left(x^i\\right)\\left(\\varphi^T\\left(x^i\\right)\\omega-t^i\\right)
122+
= 0
123+
\\end{aligned}
124+
$$
125+
$$
126+
\\begin{pmatrix}
127+
\\varphi_j\\left(x^1\\right) & \\dots & \\varphi_j\\left(x^m\\right)
128+
\\end{pmatrix}
129+
\\begin{pmatrix}
130+
\\varphi_1\\left(x^1\\right) & \\dots & \\varphi_n\\left(x^1\\right)\\\\
131+
\\vdots & \\ddots & \\vdots\\\\
132+
\\varphi_1\\left(x^m\\right) & \\dots & \\varphi_n\\left(x^m\\right)
133+
\\end{pmatrix}
134+
\\begin{pmatrix}
135+
\\omega_1\\\\
136+
\\vdots\\\\
137+
\\omega_n
138+
\\end{pmatrix}
139+
=
140+
\\begin{pmatrix}
141+
\\varphi_j\\left(x^1\\right) & \\dots & \\varphi_j\\left(x^m\\right)
142+
\\end{pmatrix}
143+
\\begin{pmatrix}
144+
t^1\\\\
145+
\\vdots\\\\
146+
t^m
147+
\\end{pmatrix}
148+
$$
149+
150+
Assuming that $T = (t^1, t^2, ..., t^m)^T$,$X = \\left(\\varphi(x^1), \\varphi(x^2), ..., \\varphi(x^m)\\right)^T$, then
151+
$$
152+
X^TX\\omega = X^TT
153+
$$`;
154+
119155
test("Latex - Equations don't have $$", () => {
120156
const result = fixLatexEquations(markdown1);
121157
expect(result).to.be.equal(output1, 'Result is incorrect');
@@ -153,4 +189,9 @@ This expression $\\sqrt{3x-1}+(1+x)^2$ is an example of a TeX inline equation in
153189
const result = fixLatexEquations(markdown5);
154190
expect(result).to.be.equal(output5, 'Result is incorrect');
155191
});
192+
193+
test('Latex - Multiple /begins inside $$', () => {
194+
const result = fixLatexEquations(markdown6);
195+
expect(result).to.be.equal(markdown6, 'Result should not have changed');
196+
});
156197
});

0 commit comments

Comments
 (0)