From d8c0787ab668e27b32ee7eeb91596daea2bb09b7 Mon Sep 17 00:00:00 2001 From: David Kutugata Date: Thu, 24 Oct 2019 16:52:58 -0700 Subject: [PATCH 1/5] Add '$$' at the beginning and end of empty latex equations to have them render correctly. --- news/2 Fixes/7992.md | 1 + .../interactive-common/cellOutput.tsx | 41 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 news/2 Fixes/7992.md diff --git a/news/2 Fixes/7992.md b/news/2 Fixes/7992.md new file mode 100644 index 000000000000..b5acc8d9dd92 --- /dev/null +++ b/news/2 Fixes/7992.md @@ -0,0 +1 @@ +Can now include a LaTeX-style equation without surrounding the equation with '$' in a markdown cell. diff --git a/src/datascience-ui/interactive-common/cellOutput.tsx b/src/datascience-ui/interactive-common/cellOutput.tsx index c3e978f0a670..c0e9f8785853 100644 --- a/src/datascience-ui/interactive-common/cellOutput.tsx +++ b/src/datascience-ui/interactive-common/cellOutput.tsx @@ -172,13 +172,52 @@ export class CellOutput extends React.Component { private renderMarkdownOutputs = () => { const markdown = this.getMarkdownCell(); // React-markdown expects that the source is a string - const source = concatMultilineStringInput(markdown.source); + const source = this.fixLatexEquations(concatMultilineStringInput(markdown.source)); const Transform = transforms['text/markdown']; const MarkdownClassName = 'markdown-cell-output'; return [
]; } + // Adds '$$' to latex formulas that don't have a '$', allowing users to input the formula directly. + private fixLatexEquations(input: string): string { + const block = '\n$$\n'; + + const beginIndexes = this.getAllIndexesOf(input, '\\begin'); + const endIndexes = this.getAllIndexesOf(input, '\\end'); + + for (let i = 0; i < beginIndexes.length; i += 1) { + const endOfEnd = input.indexOf('}', endIndexes[i] + 1); + + // Edge case, if the input starts with the latex formula we add the block at the beggining. + if (beginIndexes[i] === 0 && input[beginIndexes[i]] === '\\') { + input = block + input.slice(0, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length - 1); + // Normal case, if the latex formula starts with a '$' we don nothing. + // Otherwise, we insert the block at the beginning and ending of the latex formula. + } else if (input[beginIndexes[i] - 1] !== '$') { + input = input.slice(0, beginIndexes[i] - 1) + block + input.slice(beginIndexes[i], endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length - 1); + } + } + + return input; + } + + private getAllIndexesOf(arr: string, value: string): number[] { + const indexes = []; + + for (let i = -1;;) { + i = arr.indexOf(value, i + 1); + + if (i === -1) { + break; + } else { + indexes.push(i); + } + } + + return indexes; + } + // tslint:disable-next-line: max-func-body-length private transformOutput(output: nbformat.IOutput): ICellOutput { // First make a copy of the outputs. From a2462680824e496ed484936fcc0947eedfd82a9a Mon Sep 17 00:00:00 2001 From: David Kutugata Date: Fri, 25 Oct 2019 15:09:00 -0700 Subject: [PATCH 2/5] Moved changes to its own file. Started using Regular Expressions instead of indexOf Added unit tests --- .../interactive-common/cellOutput.tsx | 57 ++-------- .../interactive-common/latexManipulation.ts | 36 +++++++ .../latexManipulation.unit.test.ts | 102 ++++++++++++++++++ 3 files changed, 147 insertions(+), 48 deletions(-) create mode 100644 src/datascience-ui/interactive-common/latexManipulation.ts create mode 100644 src/test/datascience/latexManipulation.unit.test.ts diff --git a/src/datascience-ui/interactive-common/cellOutput.tsx b/src/datascience-ui/interactive-common/cellOutput.tsx index c0e9f8785853..5f3450df4147 100644 --- a/src/datascience-ui/interactive-common/cellOutput.tsx +++ b/src/datascience-ui/interactive-common/cellOutput.tsx @@ -1,19 +1,12 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. 'use strict'; -import '../../client/common/extensions'; - -// tslint:disable-next-line: no-var-requires no-require-imports -const ansiToHtml = require('ansi-to-html'); - import { nbformat } from '@jupyterlab/coreutils'; import { JSONObject } from '@phosphor/coreutils'; import ansiRegex from 'ansi-regex'; -// tslint:disable-next-line: no-require-imports -import cloneDeep = require('lodash/cloneDeep'); import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api'; import * as React from 'react'; - +import '../../client/common/extensions'; import { concatMultilineStringInput, concatMultilineStringOutput } from '../../client/datascience/common'; import { Identifiers } from '../../client/datascience/constants'; import { CellState } from '../../client/datascience/types'; @@ -22,9 +15,16 @@ import { noop } from '../../test/core'; import { Image, ImageName } from '../react-common/image'; import { ImageButton } from '../react-common/imageButton'; import { getLocString } from '../react-common/locReactSide'; +import { fixLatexEquations } from './latexManipulation'; import { ICellViewModel } from './mainState'; import { displayOrder, richestMimetype, transforms } from './transforms'; +// tslint:disable-next-line: no-var-requires no-require-imports +const ansiToHtml = require('ansi-to-html'); + +// tslint:disable-next-line: no-require-imports +import cloneDeep = require('lodash/cloneDeep'); + interface ICellOutputProps { cellVM: ICellViewModel; baseTheme: string; @@ -172,52 +172,13 @@ export class CellOutput extends React.Component { private renderMarkdownOutputs = () => { const markdown = this.getMarkdownCell(); // React-markdown expects that the source is a string - const source = this.fixLatexEquations(concatMultilineStringInput(markdown.source)); + const source = fixLatexEquations(concatMultilineStringInput(markdown.source)); const Transform = transforms['text/markdown']; const MarkdownClassName = 'markdown-cell-output'; return [
]; } - // Adds '$$' to latex formulas that don't have a '$', allowing users to input the formula directly. - private fixLatexEquations(input: string): string { - const block = '\n$$\n'; - - const beginIndexes = this.getAllIndexesOf(input, '\\begin'); - const endIndexes = this.getAllIndexesOf(input, '\\end'); - - for (let i = 0; i < beginIndexes.length; i += 1) { - const endOfEnd = input.indexOf('}', endIndexes[i] + 1); - - // Edge case, if the input starts with the latex formula we add the block at the beggining. - if (beginIndexes[i] === 0 && input[beginIndexes[i]] === '\\') { - input = block + input.slice(0, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length - 1); - // Normal case, if the latex formula starts with a '$' we don nothing. - // Otherwise, we insert the block at the beginning and ending of the latex formula. - } else if (input[beginIndexes[i] - 1] !== '$') { - input = input.slice(0, beginIndexes[i] - 1) + block + input.slice(beginIndexes[i], endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length - 1); - } - } - - return input; - } - - private getAllIndexesOf(arr: string, value: string): number[] { - const indexes = []; - - for (let i = -1;;) { - i = arr.indexOf(value, i + 1); - - if (i === -1) { - break; - } else { - indexes.push(i); - } - } - - return indexes; - } - // tslint:disable-next-line: max-func-body-length private transformOutput(output: nbformat.IOutput): ICellOutput { // First make a copy of the outputs. diff --git a/src/datascience-ui/interactive-common/latexManipulation.ts b/src/datascience-ui/interactive-common/latexManipulation.ts new file mode 100644 index 000000000000..17f72bec181d --- /dev/null +++ b/src/datascience-ui/interactive-common/latexManipulation.ts @@ -0,0 +1,36 @@ +// Adds '$$' to latex formulas that don't have a '$', allowing users to input the formula directly. +function fixLatexEquations(input: string): string { + const block = '\n$$\n'; + + const beginIndexes = getAllIndexesOfRegex(input, /\\begin/g); + const endIndexes = getAllIndexesOfRegex(input, /\\end/g); + + for (let i = 0; i < beginIndexes.length; i += 1) { + const endOfEnd = input.indexOf('}', endIndexes[i] + 1 + 8 * i); + + // Edge case, if the input starts with the latex formula we add the block at the beggining. + if (beginIndexes[i] === 0 && input[beginIndexes[i]] === '\\') { + input = block + input.slice(0, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length); + // Normal case, if the latex formula starts with a '$' we don't do anything. + // Otherwise, we insert the block at the beginning and ending of the latex formula. + } else if (input[beginIndexes[i] - 1] !== '$') { + input = input.slice(0, beginIndexes[i] + 8 * i) + block + input.slice(beginIndexes[i] + 8 * i, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length); + } + } + + return input; +} + +function getAllIndexesOfRegex(arr: string, value: RegExp): number[] { + const indexes = []; + let result; + + // tslint:disable-next-line: no-conditional-assignment + while ((result = value.exec(arr)) !== null) { + indexes.push(result.index); + } + + return indexes; +} + +export { fixLatexEquations }; diff --git a/src/test/datascience/latexManipulation.unit.test.ts b/src/test/datascience/latexManipulation.unit.test.ts new file mode 100644 index 000000000000..f20e044b8ec3 --- /dev/null +++ b/src/test/datascience/latexManipulation.unit.test.ts @@ -0,0 +1,102 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +'use strict'; +import { expect } from 'chai'; +import { fixLatexEquations } from '../../datascience-ui/interactive-common/latexManipulation'; + +// tslint:disable: max-func-body-length +suite('Data Science - LaTeX Manipulation', () => { + const markdown1 = `\\begin{align} +\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\ +\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\ +\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 +\\end{align} +sample text`; + + const output1 = ` +$$ +\\begin{align} +\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\ +\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\ +\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 +\\end{align} +$$ + +sample text`; + + const markdown2 = `$\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*}$ +sample text +$\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*}$ +sample text`; + + const markdown3 = `\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +sample text +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +sample text +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +sample text + +sample text +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*}`; + + const output3 = ` +$$ +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +$$ + +sample text + +$$ +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +$$ + +sample text + +$$ +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +$$ + +sample text + +sample text + +$$ +\\begin{align*} +(a+b)^2 = a^2+2ab+b^2 +\\end{align*} +$$ +`; + + test('Latex - Equations don\'t have $$', () => { + const result = fixLatexEquations(markdown1); + expect(result).to.be.equal(output1, 'Result is incorrect'); + }); + + test('Latex - Equations have $', () => { + const result = fixLatexEquations(markdown2); + expect(result).to.be.equal(markdown2, 'Result is incorrect'); + }); + + test('Latex - Multiple equations don\'t have $$', () => { + const result = fixLatexEquations(markdown3); + expect(result).to.be.equal(output3, 'Result is incorrect'); + }); +}); From d6f37794552909bc274efd3d3617b8b47428f8e8 Mon Sep 17 00:00:00 2001 From: David Kutugata Date: Fri, 25 Oct 2019 16:05:41 -0700 Subject: [PATCH 3/5] added missing copyright --- src/datascience-ui/interactive-common/latexManipulation.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/datascience-ui/interactive-common/latexManipulation.ts b/src/datascience-ui/interactive-common/latexManipulation.ts index 17f72bec181d..c03daf6e2e72 100644 --- a/src/datascience-ui/interactive-common/latexManipulation.ts +++ b/src/datascience-ui/interactive-common/latexManipulation.ts @@ -1,3 +1,5 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. // Adds '$$' to latex formulas that don't have a '$', allowing users to input the formula directly. function fixLatexEquations(input: string): string { const block = '\n$$\n'; From 65625a2970359535eb7cabc39550d46ec02609a2 Mon Sep 17 00:00:00 2001 From: David Kutugata Date: Mon, 28 Oct 2019 14:31:09 -0700 Subject: [PATCH 4/5] Made the 2 regex expressions a bit more specific, it now includes the opening anc closing brackets, with whatever name the user chooses. Also, the code will run only if the amount of indexes are the same. --- .../interactive-common/latexManipulation.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/datascience-ui/interactive-common/latexManipulation.ts b/src/datascience-ui/interactive-common/latexManipulation.ts index c03daf6e2e72..0f2e060915aa 100644 --- a/src/datascience-ui/interactive-common/latexManipulation.ts +++ b/src/datascience-ui/interactive-common/latexManipulation.ts @@ -4,19 +4,21 @@ function fixLatexEquations(input: string): string { const block = '\n$$\n'; - const beginIndexes = getAllIndexesOfRegex(input, /\\begin/g); - const endIndexes = getAllIndexesOfRegex(input, /\\end/g); - - for (let i = 0; i < beginIndexes.length; i += 1) { - const endOfEnd = input.indexOf('}', endIndexes[i] + 1 + 8 * i); - - // Edge case, if the input starts with the latex formula we add the block at the beggining. - if (beginIndexes[i] === 0 && input[beginIndexes[i]] === '\\') { - input = block + input.slice(0, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length); - // Normal case, if the latex formula starts with a '$' we don't do anything. - // Otherwise, we insert the block at the beginning and ending of the latex formula. - } else if (input[beginIndexes[i] - 1] !== '$') { - input = input.slice(0, beginIndexes[i] + 8 * i) + block + input.slice(beginIndexes[i] + 8 * i, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length); + const beginIndexes = getAllIndexesOfRegex(input, /\\begin\{[a-z]*\*?\}/g); + const endIndexes = getAllIndexesOfRegex(input, /\\end\{[a-z]*\*?\}/g); + + if (beginIndexes.length === endIndexes.length) { + for (let i = 0; i < beginIndexes.length; i += 1) { + const endOfEnd = input.indexOf('}', endIndexes[i] + 1 + 8 * i); + + // Edge case, if the input starts with the latex formula we add the block at the beggining. + if (beginIndexes[i] === 0 && input[beginIndexes[i]] === '\\') { + input = block + input.slice(0, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length); + // Normal case, if the latex formula starts with a '$' we don't do anything. + // Otherwise, we insert the block at the beginning and ending of the latex formula. + } else if (input[beginIndexes[i] - 1] !== '$') { + input = input.slice(0, beginIndexes[i] + block.length * 2 * i) + block + input.slice(beginIndexes[i] + block.length * 2 * i, endOfEnd + 1) + block + input.slice(endOfEnd + 1, input.length); + } } } From 2d4380d3cb800c4b2cd6fc25369208f45a686976 Mon Sep 17 00:00:00 2001 From: David Kutugata Date: Mon, 28 Oct 2019 15:54:20 -0700 Subject: [PATCH 5/5] changed the way the function is exported. --- src/datascience-ui/interactive-common/latexManipulation.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/datascience-ui/interactive-common/latexManipulation.ts b/src/datascience-ui/interactive-common/latexManipulation.ts index 0f2e060915aa..e848ffaf1571 100644 --- a/src/datascience-ui/interactive-common/latexManipulation.ts +++ b/src/datascience-ui/interactive-common/latexManipulation.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // Adds '$$' to latex formulas that don't have a '$', allowing users to input the formula directly. -function fixLatexEquations(input: string): string { +export function fixLatexEquations(input: string): string { const block = '\n$$\n'; const beginIndexes = getAllIndexesOfRegex(input, /\\begin\{[a-z]*\*?\}/g); @@ -36,5 +36,3 @@ function getAllIndexesOfRegex(arr: string, value: RegExp): number[] { return indexes; } - -export { fixLatexEquations };