Giter VIP home page Giter VIP logo

Comments (7)

korsbo avatar korsbo commented on August 25, 2024 1

Thanks for your question/suggestion, I do appreciate getting input.

It is possible, but it is a different beast than going from julia to LaTeX. Latexify.jl uses the structure of the julia Expr type to precisely infer the correct latex equivalent (see internal workings). The inverse would not work the same, since a string does not have any structure that can be used for this.

That being said, it is still possible.
I think that the easiest way of doing this could be to use regex to format a latex string to something that Base.parse() can work with. Once you have that, you are pretty much done.

After some tinkering I created a basic version which can deal with addition, subtraction, multiplications and fractions (the same operations Latexify.jl started with):

For this I needed a function which can deal with \frac{}{} and do some fancy bracket matching (i found that part of the regex here).

"""
    replace_frac(s::String)

recursively find and replace \\frac{...}{...} with (...)/(...).
"""

function replace_frac(s::String)
    r_frac = r"\\frac\{((?>[^{}]|(?R))*)\}\{((?>[^{}]|(?R))*)\}"
    r_match = match(r_frac, s)
    r_match isa Void && return s
    
    newfrac = "(" * join([m for m in match(r_frac, s).captures], ")/(")* ")" 
    s = replace(s, r_match.match, newfrac)
    return replace_frac(s)
end

Now, we generate a latex string of the type String:

l = latexify("(x/(b*(y+a))+b)*a + x/y + b*(x+b)") 
s = l.s

This can be converted to a julia expression with:

s = replace_frac(s)
s = join(split(s, "\$"))
s = join(split(s, "\\left"))
s = join(split(s, "\\right"))
s = replace(s, "\{", "\(")
s = replace(s, "\}", "\)")
s = replace(s, "\\cdot", "*")

expr = parse(s)

latexify(expr)

And we see from the output of the last latexify() call that we have a new expression which is equivivalent to the one we started with.

So, it can be done. But is it useful? When would this be a nice feature to have?

from latexify.jl.

TravisA9 avatar TravisA9 commented on August 25, 2024

But is it useful? When would this be a nice feature to have?

I was thinking about the great number of researchers that I work with who do not know how to code or at least are not familiar with julia. Some are pretty proficient in math though and I have had to trudge through some pretty cryptic manuscripts to review the English. If I could tell them that they could parse their math and have working (or at least nearly working) code that maybe a big win for julia.
Also, for those of us who are not as proficient at deciphering math notation but read code well, I think it could be useful.

I would fully expect some formulas to be a bit more difficult to transpile to julia but something is better than nothing. Also, I know that there would be a lot of blanks to fill in to optimize the code.

I don't know if it's worth doing or not but I spent yesterday exploring the idea ...fiddling with code.

Thanks for your reply @korsbo

from latexify.jl.

korsbo avatar korsbo commented on August 25, 2024

I still do not quite see the "market" for this. I would imagine that a non-specialists would have even more problems with dealing with julia Expr's than with printed equations. Sure, we could also provide a macro for turning that expression into a function, but I have trouble seeing how such a process could be robust. In order to please the demography that you are suggesting we would have to:

  1. Create an UnLatexify which can convert most published maths to expressions.
  2. Turn these expressions into something that can be solved (integration with the DiffEq suite?).

I see problems with all of these steps. The problems are not insurmountable, but they will require a very substantial time investment.

The first point may be the easiest to solve, but it is still problematic in the "most" part. Simple operations are fine (as demonstrated), but the slightly more complicated ones will probably have to be solved one-by-one. Sums, for example, could be a pain to deal with (I have no way of even going from a for-loop to LaTeX, and I would think that the reverse is more difficult).

The second point would also be easy in the very first, crude, implementation but for anything more, it would require writing something like the ParameterizedFunctions.jl or DiffEqBiological packages. Doable, but time-consuming.

Also, It would be nice to go from a pdf to latex, but who has access to the .tex document of a paper (are you an editor)?

So, extending latexify to be able to unlatexify simple stuff ~a couple of hours. Making it suitable for non-specialists to evaluate maths in papers ~weeks. Making it appealing to non-nerds ~no upper time limit.

I could be convinced to do the first bit, but the subsequent time investment to a finished "product" is more than I would like to take on. I could however point anyone willing in the right directions.

from latexify.jl.

TravisA9 avatar TravisA9 commented on August 25, 2024

I think your assessment of the situation is right on.

...but who has access to the .tex document of a paper (are you an editor)?

Yes, in my case I review (make corrections to) articles that are to be submitted for publishing and some of them come in .tex format. Some researchers deal purely in theory(graph, etc.) and never actually program but they show their maths. Others do make software but translate the algorithms from say, C++ to mathematical notation and include that in their manuscripts.

I have encouraged many to take a look at julia but it's often hard for people to find the motivation to find the time or motivation to expand into new areas.

On the other hand quite a few people I have talked to have started using julia and are very glad they have so... great for them!

Well, I'm glad this at least got some discussion/consideration. Who knows, I (or someone else) may find time in some future to try to put something together.

from latexify.jl.

korsbo avatar korsbo commented on August 25, 2024

I'm happy to have had the discussion, it raises some nice possibilities. I would imagine that the threshold for actually getting this done will decrease as the package ecosystem of Julia evolves (especially the DiffEq suite, that stuff is amazing). One day it may be worth doing.

from latexify.jl.

Datseris avatar Datseris commented on August 25, 2024

Just chiming in to say that this is a cool feature that would be nice to have. But I understand that we live in the real world with finite time and resources, while this seems to be an extremely difficult problem to get right.


I just wanted to add something to the "market" that is available for this. I am not an editor, nor did I get the expressions I want from latex. I got them from Mathematica. I am a theoretical physicists and often I do my analytics with Mathematica. But after I got the analytic result I need, Mathematica is a ridicoulous language to do any kind of computation with it. So I transform it into Julia. :)

from latexify.jl.

Datseris avatar Datseris commented on August 25, 2024

Ah, I just saw : https://github.com/JuliaInterop/Mathematica.jl I will check it out, maybe it already has the power to do what I want to do (convert an analytic result from Mathematica into a Julia function).

from latexify.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.