Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to parsing incomplete Latex expressions #5

Open
mindreframer opened this issue Apr 10, 2021 · 6 comments
Open

Support to parsing incomplete Latex expressions #5

mindreframer opened this issue Apr 10, 2021 · 6 comments
Assignees
Labels
feature New feature or enhancement

Comments

@mindreframer
Copy link

Parsing incomplete (mathematically) expressions seems to leave some parts outs, eg:

parse("2 * 2 = ")

=> [ 'Multiply', { num: '2' }, { num: '2' } ]

parse("2 * 2 = 4")
=> [ 'Equal', [ 'Multiply', { num: '2' }, { num: '2' } ], { num: '4' } ]

This has the implication that chained parse / serialize calls would alter the initial input...

Will this ever work with in-complete expressions?

@mindreframer
Copy link
Author

test case:

import { expect } from "@jest/globals";
import { parse, serialize } from "@cortex-js/math-json";

function roundtrip(input: string) {
  return serialize(parse(input));
}
describe("parse", () => {
  it("changes input on roundtrip", () => {
    expect(roundtrip("2\\times2=4")).toBe("2\\times2=4");
    expect(roundtrip("2\\times2=")).toBe("2\\times2"); // misses trailing `=` sign
  });
});

@arnog
Copy link
Member

arnog commented Apr 10, 2021

In general, there is no round-tripping guarantee. The Latex expression could include extra whitespace, comments or other syntactic idioms that are not preserved in the MathJSON expression. For example, both \frac12 and \frac{1}{2} result in the same MathJSON expression.

That said, in this particular example, I can see a case for attempting to preserve the "partial" expression. FYI, it currently returns a missing-operand error. Instead, it could return [ 'Equal', [ 'Multiply', 2, 2 ], 'Missing']. This would then serialize to 2 \times 2 = \placeholder.

@arnog arnog self-assigned this Apr 10, 2021
@arnog arnog added the feature New feature or enhancement label Apr 10, 2021
@mindreframer
Copy link
Author

I see. Maybe this could be a configurable element. A placeholder has already different meaning and might be used for other purposes. Or - the missing operand could serialize to an space char. That would retain the input expression semantically. I think this would work out for my situation and would provide sound behavior. Wdyt?

@arnog
Copy link
Member

arnog commented Apr 10, 2021

You can configure how the Missing symbol gets serialized:

const customLatex = new LatexSyntax( { dictionary: [
    ...LatexSyntax.getDictionary(), 
    { name: 'Missing', serialize: '\\placeholder'}
] } );
customLatex.serialize(['Equal', 'x', 'Missing']);

Replace '\\placeholder' with the desired output, including an empty string.

Does that work for you?

@mindreframer
Copy link
Author

Hey @arnog , it surely would! Only I'm not getting the "Missing" symbol, when I parse the expression

let res = parse("2\\times2=")
// ["Multiply",{"num":"2"},{"num":"2"}]

Do I have to configure something for this?

@arnog
Copy link
Member

arnog commented Apr 10, 2021

No, that's the current behavior, but I'm going to change it to use the Missing symbol in this case. Stay tuned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or enhancement
Development

No branches or pull requests

2 participants