# Could MathTeXEngine hook up into LaTeX?

**URL:** https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742
**Category:** Visualization
**Tags:** latex
**Created:** [August 20, 2021, 4:41pm UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742 "2021-08-20T16:41:07Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [August 20, 2021, 4:41pm UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/1 "2021-08-20T16:41:07Z")

</div>

This topic might be quite irritating, so sorry for that in advance.

Currently, `Makie.jl` uses `MathTeXEngine.jl` to process LaTeX. I have looked at `MathTeXEngine.jl`, and what I understood is that it returns information about a set of boxes: coordinates and scalings for the boxes plus the symbols to go inside the boxes.

As it is written in the Readme, `MathTeXEngine` parses the LaTeXString and then produces the approximation for the LaTeX output using the unicode characters. As far as I understand, this approximation needs to be hardcoded manually, which is a time-consuming process.

So, I have two question:

- Why was the design choice made to do everything manually: parsing, approximating with unicode, etc. Would it not be easier just to use LaTeX directly like matplotlib does?

- May it be a good idea to provide an alternative which actually hooks up into LaTeX and uses the same syntax as `MathTeXEngine`? I looked into `matplotlib` source code: they process LaTeX code with LaTeX, and then parse the `.dvi` output. Also, it looks like dvi contains precisely the kind of information that we need: font, boxes with coordinates and scalings, symbols. Also, it may be much easier to parse dvi instead of the source tex code.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 21, 2021, 11:22am UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/2 "2021-08-21T11:22:13Z")

</div>

[https://github.com/JuliaPlots/MakieTeX.jl](https://github.com/JuliaPlots/MakieTeX.jl) does that… But without the DVI parsing part, since that’s pretty complicated…  
The simple answer is: It’s super hard to ship latex with a package, and then make it spit out the correct text layout information… Certainly not impossible, but for the LaTeX support we ship natively with Makie, an approach like MathTeXEngine is much preferred.

Anyways, feel free to implement the dvi parsing, i’d be very happy to have that 🙂

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [August 22, 2021, 10:31pm UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/3 "2021-08-22T22:31:35Z")

</div>

> [@sdanisch](#):
>
> But without the DVI parsing part, since that’s pretty complicated…

Actually, I have looked through matplotlib source code, and this DVI parsing part is rather straightforward. DVI file contains simple commands, like “go down one line”, or “put a character from some font and move right by its width”. The result of the parsing gives you the coordinates of the symbols with the indices of the symbols inside the used fonts.

The hard part is linking with the font files (like ttf or otf) to get the actual glyphs for rendering. TeX predates the vector fonts. As a result, working with the modern fonts is a bit tricky, but not impossible (it seems pdfTeX ships with a file “pdftex.map” containing mapping from internal TeX fonts to more modern fonts).

> [@sdanisch](#):
>
> It’s super hard to ship latex with a package

Hooking up with LaTeX is really needed only if you need to produce publish quality plots. However, if you are trying to write a paper, you will have LaTeX already installed in the first place to do the writing.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [August 23, 2021, 5:58am UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/4 "2021-08-23T05:58:53Z")

</div>

I’d also be glad to have that feature. If you could come up with a prototype that shows a path forward with real Latex, that would be great.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [August 23, 2021, 6:38am UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/5 "2021-08-23T06:38:37Z")

</div>

It may be interesting to look at implementations of [MathML Core](https://www.w3.org/TR/2021/WD-mathml-core-20210810/), the markup language for math under development for a W3C standard.

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 23, 2021, 10:43am UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/6 "2021-08-23T10:43:07Z")

</div>

> [@Gregstrq](#):
>
> Actually, I have looked through matplotlib source code, and this DVI parsing part is rather straightforward.

That’s good news, let us know whenever you have a PR ready 🙂

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [August 23, 2021, 11:00am UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/7 "2021-08-23T11:00:20Z")

</div>

> [@sdanisch](#):
>
> let us know whenever you have a PR ready

It is straightforward, but it may take some time. (I have other commitments as well.)

@sdanisch I could use some expertise from MathTeXEngine. If I have questions about its internals, should I ask you, @Kolaru, or someone else? Also, do you guys have a dedicated workspace on Slack? (Or should I simply write in Julia#makie channel?)

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [August 23, 2021, 11:25am UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/8 "2021-08-23T11:25:21Z")

</div>

Yeah @Kolaru is the person to ask 😉 Not sure where on slack would be best, makie channel seems fine…  
If you write anything, I think it should ultimately go into MakieTex.jl, but I’d write a simple prototype outside of it first, since MakieTex.jl is not in a really good state 😃 I can help integrate it once DVI → makie plot is working.

---

<div class="post-metadata">

### Author: ![Kolaru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kolaru/32/4574_2.png) [@Kolaru](https://discourse.julialang.org/u/Kolaru)
#### Post date: [August 23, 2021, 12:55pm UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/9 "2021-08-23T12:55:41Z")

</div>

> [@Gregstrq](#):
>
> Why was the design choice made to do everything manually: parsing, approximating with unicode, etc. Would it not be easier just to use LaTeX directly like matplotlib does?

First I love writing parsers 😃

But more seriously there were two main reasons:

- I needed the feature and I don’t understand latex internal process. Comparatively, parsing latex and placing characters seemed rather straightforward (still a lot of work, but no need to learn several other programming languages to go through).
- Implementing it felt unavoidable. At some point for speed or ease of use, a native solution would be needed. This idea was reinforced by the fact that matplotlib also ships its own simple latex engine.

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [August 23, 2021, 3:13pm UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/10 "2021-08-23T15:13:50Z")

</div>

> [@Kolaru](#):
>
> This idea was reinforced by the fact that matplotlib also ships its own simple latex engine.

After more roaming through matplotlib internals I gathered the following:  
matplotlib has simple latex engine as well as the means to hook up into latex. Simple latex engine is used with interactive backends like ‘Agg’, for example. Hooking up into LaTeX is used with static backends, like pdf or svg.

Here goes the tricky part. Let’s say you use a interactive backend. Say, ‘Agg’, which is used by default. If you try to save figure to a pdf file, matplotlib actually switches backend to pdf and then uses this backend to save a file.

To summarize: matplotlib uses simple latex engine for interactive plotting and raster outputs. In the case of vector graphics output, matplotlib hooks into LaTeX.

---

<div class="post-metadata">

### Author: ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)
#### Post date: [August 23, 2021, 3:15pm UTC](https://discourse.julialang.org/t/could-mathtexengine-hook-up-into-latex/66742/11 "2021-08-23T15:15:31Z")

</div>

I would say that we need both MathTeXEngine and the means to hook up directly into LaTeX: one can be used for interactive plotting, another — for publication quality output.
