 # From LaTeX to Julia

Hey everyone,

my current workflow consists of writing my paper in LyX and then copying the formulas to Julia. This is however often prone to errors. Ideally, I would like to copy my equation from Latex directly to Julia. For example, it could look like this

\frac{b}{\sqrt{2\pi\left(1+b^{2}\right)}}


in a simple case. Without having tried yet, it does not seem too difficult to parse this and automatically get the corresponding code in Julia. Before I am having a go at it, I wanted to know if anyone else would be interested or is aware of such a project?

Thank you very much!

2 Likes

My guess is that the other direction (julia to latex) will be much easier.

1 Like

Maybe you can try to use SymPy.jl . There seems to be some latex parsing functionality in sympy, which I assume you can call from SymPy.jl .
Then you could convert the SymPy expression to Julia expression.

You can certainly convert Julia into \LaTeX, there are various packages that support that, including the symbolic package Reduce.jl as well as other packages:

julia> using Reduce; Algebra.latex(:(b/sqrt(2π*(1+b^2))))
"\\begin{displaymath}\nb/\\left(\\sqrt {\\pi } \\sqrt {b^{2}+1} \\sqrt {2}\\right)\n\\end{displaymath}\n"

julia> println(ans)
\begin{displaymath}
b/\left(\sqrt {\pi } \sqrt {b^{2}+1} \sqrt {2}\right)
\end{displaymath}


But I am not aware of any full \LaTeX to Julia parsers yet, although I have a VerTeX.jl package which can parse some document level \LaTeX code, but does not currently handle expression level parsing. It would certainly be nice to have a parser which converts those equation expressions into Julia.

3 Likes

I very like your idea and I certainly would like to use it, if it was possible.

If I can use Latex symbol, why I can’t use latex equation? It will be cool.

You could, for a subset of equations (not all equations can be mapped to code), it’s just no one has written a parser for that yet AFAIK.

2 Likes

Since I do not have a proper idea on how to do it, is there someone who I could talk to, to get a general idea?

My idea so far to build it as an atom-package. You have LaTeX in your clipboard, press for example CTRL+SHIFT+V, then what is in your clipboard is parsed and in combination with the latex-completions package it is turned into Julia code. Would that the general way to go be? Are there any tutorials/sources I should read first?

Generally you would need to implement a LaTeX parser (the most difficult part I guess), map to a Julia AST, which would then be printed for you nicely as Julia code.

I don’t think it is a trivial task — I would recommend either just translating those formulas manually, or, as others have suggested, going in the other direction.

1 Like

It would be fairly easy to build a string macro that would turn

LaTeX"\frac{b}{\sqrt{2\pi\left(1+b^{2}\right)}}"


into

frac(b, sqrt(2 * π * (1 + b ^ 2))))


Before using this macro, one would have to define some functions, depending on context. For example, one might define frac(x,y) = x/y.

4 Likes

I’m interested in writing a parser like this if nobody else has, but don’t know yet when I might give it a try.