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

How to convert division symbol (u+00f7) or chr(247) to latex using py2tex? #63

Open
shashikulk opened this issue Apr 4, 2023 · 8 comments

Comments

@shashikulk
Copy link

In one of the maths equations, I am having division symbol (u+00f7) or chr(247) as below -

txt1 = '3/4' + ' ' + chr(247) + ' ' + '8/15'

When I use py2tex to convert it into latex, I am getting below error -

SyntaxError: invalid character '÷' (U+00F7)

Can anyone please suggest / help how to convert this character / such maths characters to LaTex using py2tex?

@erwanp
Copy link
Owner

erwanp commented Apr 5, 2023

This is not currently supported but you can add it in Pytexit.
There is a look-up conversion table for unicode : https://github.com/erwanp/pytexit/blob/master/pytexit/core/core.py#L15
Try to add it there ; and replace it by "/" ?

Also add a Test if you want to make sure your change remains forever https://github.com/erwanp/pytexit/blob/master/pytexit/test/test_functions.py

(note : in your text above make sure you have correct parenthesis)

@WesDH
Copy link
Contributor

WesDH commented Apr 14, 2023

Hi there, let me volunteer to add in support for this. Thanks for the tips on getting started!

@WesDH
Copy link
Contributor

WesDH commented Apr 23, 2023

To follow up with the original poster, I think his intention was for getting this Latex formula output:

3/4 \div 8/15
image

However, adding to the Unicode table:
"÷": "div",
Doesn't quite work, as the Python function ast.parse() tries to evaluate and see if the following string can be executed as a valid Python expression: 3/4div8/15. For which ast.parse() throws an error of invalid syntax, since it is not a valid Python expression.

The best I think we can do is adding to the Unicode table:
"÷": "/",

At least then it can accept the Unicode input:
py2tex("(3/4)" + "÷" + "(8/15)")

Which would give the following as output, if not ideal:
\frac{\frac{3}{4}}{\frac{8}{15}}
image

Let me know if this is sufficient, and I can put in a pull request.

@shashikulk
Copy link
Author

Thanks Wes for your response and analysis of possible solution.
Yes, the intention is to capture "÷" symbol as is in the Latex/final output, so that it is more readable and familiar to the target audience (school students).
The below LaTex representation and corresponding output may not be preferred by my client as it is not considered elegant way of displaying the expression, even if it is mathematically correct.

\frac{\frac{3}{4}}{\frac{8}{15}}

Currently, I am using a workaround solution in which I am handling "÷" symbol outside py2tex as text. But it will be great if we get a clean solution using pytexit / py2tex, if that is possible.

@WesDH
Copy link
Contributor

WesDH commented Apr 23, 2023

Understood, I had a look at this today but am still running into the issue of ast.parse(), as it must take "/" as division input, then at which time, we lose track whether it was originally a "/" or a "÷", since ast.parse() only accepts "/". This could use further looking into, perhaps it is possible but a solution will be more complicated.

In the meantime, I followed erwanp's suggestion for the unicode table: "Try to add it there ; and replace it by "/" ?"--- this works in the meantime such that at least feeding in the string txt1 = '3/4' + ' ' + chr(247) + ' ' + '8/15' can be parsed, if not ideally, at least mathematically correctly.

@WesDH
Copy link
Contributor

WesDH commented Apr 24, 2023

To follow up again, I came up with a second solution that's a bit janky but it appears to work. First, we can convert the "÷" symbol into a magic number "+2146136747+", this way, it validates in ast.parse() as a valid expression thanks to the + symbols. Second, we then ignore the + symbols by checking for this magic number on the left and right nodes. Lastly, when visit_Num(self, n) is called, we check for this magic number, and instead return "\div" instead of the number.

This appears to work, passing all tests, and I will submit a second pull request and leave it up for the project author to decide. As this is a bit more complicated and not as clean of a solution. Open to feedback/further edits if necessary.

image

@erwanp
Copy link
Owner

erwanp commented Apr 24, 2023

Hello Ivr merged your 2nd solution in #64, but looking at your comment I think we'd rather keep the 1st one. Can you reupdate the PR?

@WesDH
Copy link
Contributor

WesDH commented Apr 24, 2023

Created a new PR to reflect the 1st option.

For Shashukulk, you can to refer to this commit, here you only need to make 3 additions to core.py. If you want to run this custom logic to obtain usage of the divison symbol as you intend. Hope this helps!

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

No branches or pull requests

3 participants