# From LaTeX to Julia

**URL:** <https://discourse.julialang.org/t/from-latex-to-julia/32485>\
**Category:** Juno\
**Created:** [December 19, 2019, 4:15pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485 "2019-12-19T16:15:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [December 19, 2019, 4:15pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/1 "2019-12-19T16:15:26Z")

</div>

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

```julia
\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!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 19, 2019, 4:26pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/2 "2019-12-19T16:26:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [December 19, 2019, 4:41pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/3 "2019-12-19T16:41:45Z")

</div>

Maybe you can try to use SymPy.jl . There seems to be some [latex parsing functionality](https://docs.sympy.org/latest/modules/parsing.html?highlight=latex#experimental-latex-parsing) in sympy, which I assume you can call from SymPy.jl .  
Then you could convert the SymPy expression to Julia expression.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [December 19, 2019, 4:48pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/4 "2019-12-19T16:48:30Z")

</div>

You can certainly convert Julia into \LaTeX, there are various packages that support that, including the symbolic package [Reduce.jl](https://github.com/chakravala/Reduce.jl) as well as other packages:

```nohighlight
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](https://github.com/chakravala/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.

---

<div class="post-metadata">

**Author:** ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)\
**Post date:** [December 19, 2019, 5:15pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/5 "2019-12-19T17:15:48Z")

</div>

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 already can use \sqrt

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 20, 2019, 8:55am UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/6 "2019-12-20T08:55:58Z")

</div>

> [@BMval](#):
>
> If I can use Latex symbol, why I can’t use latex equation?

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.

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [December 20, 2019, 10:37am UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/7 "2019-12-20T10:37:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 20, 2019, 10:55am UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/8 "2019-12-20T10:55:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [December 20, 2019, 11:52am UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/9 "2019-12-20T11:52:14Z")

</div>

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

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

```

into

```julia
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`.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [December 20, 2019, 4:30pm UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/10 "2019-12-20T16:30:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![VitaminBrad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vitaminbrad/32/17350_2.png) [@VitaminBrad](https://discourse.julialang.org/u/VitaminBrad)\
**Post date:** [June 3, 2020, 11:30am UTC](https://discourse.julialang.org/t/from-latex-to-julia/32485/11 "2020-06-03T11:30:37Z")

</div>

I could certainly use this in my research, if someone does make one!.. Bump! 🙂
