mentat-collective / road-to-reality

The Road to Reality essay and newsletter source. A journey through computational physics, from eval/apply to the Einstein field equations.
https://reality.mentat.org
MIT License
88 stars 10 forks source link

LaTeX to Emmy converter #13

Open ronnyli opened 1 year ago

ronnyli commented 1 year ago

You probably thought of this already and this repo might not be the right place to talk about it, but if Emmy translates to LaTeX then it could do the opposite, which would be insane. I could open any academic paper and turn their proofs and formulas into executable code...

sritchie commented 1 year ago

I love this idea! I've done a bit of the work here already, and I'd love to hear how this experience looks /feels to you:

https://mathlive.mentat.org/

This is an equation editor like you're suggesting, with the ability to generate "MathJSON" out of the editable LaTeX: https://mathlive.mentat.org/#mathjson

The idea is that I can turn that MathJSON blob into an emmy function, so you could take that example of yours and define f using the equation editor. So where you had

(defn f [x] (* 3 (square x)))

And then (D f) below, you'd instead define f like f(x) := 3x^2, rendered as TeX.

Let me know what else you're thinking here for this experience and let's make it happen.

sritchie commented 1 year ago
image

looks like a katex bug there at the end, rendering \coloneq as :-...

ronnyli commented 1 year ago

Thank you for sharing! I think what you have is very useful for people who are already comfortable in LaTeX or with constructing equations in general. For a math noob like myself, I would be worried about making a mistake when trying to recreate something I saw in a paper.

I would be more confident in the MathJSON/Emmy output if I could copy-paste in LaTeX from somewhere else since I know there's no chance of human error. I have no idea how feasible that is!

Example: I'm reading a paper or Wikipedia article (example) and I come across a formula that I'd like to use in my own codebase:

Screenshot 2023-05-21 at 1 21 25 PM

I copy-paste the resulting string into a function that converts to Emmy:

(latex->emmy "\operatorname {Var} (X)=\sum _{i=1}^{n}p_{i}\cdot (x_{i}-\mu )^{2}")

Here's an example of GPT doing such conversion into Python. I chose Python since GPT is more likely to make mistakes in Clojure.

Screenshot 2023-05-21 at 1 24 41 PM

I like the idea of converting to Emmy for many reasons, one of which is that I can render it back into LaTeX which would be helpful to ensure that no mistakes were made in translation. When using GPT, I kind of have to hope that it worked unless I was already an expert in the formula / code.

Anyway, just an idea I had this morning. Glad you found it interesting!