Skip to content

Conversation

@m-messer
Copy link
Member

@m-messer m-messer commented Oct 28, 2025

A slightly hacky fix for E being parsed as E instead of Euler's when using latex2sympy.

Before this PR:

  • parsing of typed (text) input was fine, and remains fine.
  • parsing of draw/scan (handwriting) input was problematic if it contained 'E'
  • The problem was only with the preview, not with the evaluation
  • The problem stemmed from the latex2sympy2 library failing to parse 'E' properly
  • The latex2sympy2 library is only called when handwriting is used

After this PR:

  • typed (text) input behaves as before
  • draw/scan (handwriting) input now parses without problems when it contains 'E', and therefore previews succeed and submission is possible
  • Evaluation of the submissions behaves as before, with the difference that handwritten inputs with 'E' are now possible to submit.

Issues which resulted in this fix:

  1. latex2sympy has E hard-coded as Euler's in its tokens - would have to change the lexer to fix this
  2. latex2sympy does this for any value of E in a string, so it cannot use a placeholder such as EEE or Sym_{E}
  3. While sympy parse_latex works with E, it simplifies the expression, and results in other tests failing. And does not have any way of disabling this reliably.

I decided that in preview that all instances of E can be parsed as E instead of Euler's in Sympy. As in the preview, we are only checking if it parses, not if it is correct. Since E is passed to the evaluation function as a string, the evaluation side can then treat it as Euler's if required. If preferred, we can do this only when elementary_functions is disabled.

If you can think of any test cases that might break this approach, please let me know, and I will add them.

app/docs/dev.md Outdated

**Remark**: Since it is likely that there will be significant overlap between the response preview and the response evaluation (e.g. code for parsing and interpreting the response), it is good practice if they can share as much code as possible to ensure consistency. For this reason it might be better to move the preview functions fully inside the context (either by making a `preview` subfolder in the `context` folder, or by moving the implementation of the preview function inside the context files themselves). In this case the `preview.py` and `evaluation.py` could also share the same code for determining the right context to use.

`E` is treated as `E` in when `is_latex` is enabled. As latex2sympy2 does not parse E correctly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires a little bit more detail. Maybe at least a link to the PR?

@peterbjohnson
Copy link
Contributor

Worth clarifying in the overall PR description:

  • before this PR:
    • parsing of typed (text) input was fine, and remains fine.
    • parsing of draw/scan (handwriting) input was problematic if it contained 'E'
    • the problem was only with the preview, not with the evaluation
    • the problem stemmed from the latex2sympy2 library failing to parse 'E' properly
    • the latex2sympy2 library is only called when handwriting is used
  • after this PR:
    • typed (text) input behaves as before
    • draw/scan (handwriting) input now parses without problems when it contains 'E', and therefore previews succeed and submission is possible
    • Evaluation of the submissions behaves as before, with the difference that handwritten inputs with 'E' are now possible to submit.

@m-messer
Copy link
Member Author

Updated description with your comment and linked PR in dev notes.

@m-messer m-messer merged commit fea06f5 into main Oct 29, 2025
@m-messer m-messer deleted the fix/E_latex_preview branch October 29, 2025 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants