# Jupyter notebook: Use pgf code instead of pdf/svg image for export to latex

**URL:** <https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [January 14, 2020, 1:54pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344 "2020-01-14T13:54:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 14, 2020, 1:54pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/1 "2020-01-14T13:54:32Z")

</div>

When displaying DataFrames in a Jupyter notebook they are nicely rendered using html. When you export the notebook to latex they are nicely exported as latex code creating that table.

Is it possible to have the same functionality for plots created with PGFPlots.jl or PGFPlotsX.jl? Meaning if I export a jupyter notebook to latex I would get a latex document that uses a the pgf code to display the plot instead of including an svg or pdf image of it.

I’m not sure in which package (IJulia, ipython, nbconvert, Plots.jl, PGFPlots(X).jl or a combination of those) this would have to be implemented.

---

<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:** [January 14, 2020, 2:17pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/2 "2020-01-14T14:17:27Z")

</div>

PGFPlotsX should work fine in Jupyter notebooks, and you can save both `pdf` and `svg` images, or LaTeX source (pgfplots) directly. See the manual:

[https://kristofferc.github.io/PGFPlotsX.jl/dev/man/save.html](https://kristofferc.github.io/PGFPlotsX.jl/dev/man/save.html)

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 14, 2020, 2:33pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/3 "2020-01-14T14:33:00Z")

</div>

Yes, displaying the plots in a notebook works all fine. And converting the notebook to latex with for example `jupyter-nbconvert --to latex mynotebook.ipynb` works also fine.

However the plots are included as images (pdf or svg) in the generated latex document. I wonder if it would be possible for them to be automatically included as pgf plots/tikz code instead. Similar to how it already happens with tables.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 14, 2020, 2:42pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/4 "2020-01-14T14:42:01Z")

</div>

It might be enough for PGFPlotsX to define a `show` method for the `text/latex` MIME type.

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 14, 2020, 3:11pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/5 "2020-01-14T15:11:57Z")

</div>

A quick test of adding

```julia
Base.show(io::IO, ::MIME"text/latex", p::_SHOWABLE) = print_tex(io, p)

```

to `tikzdocument.jl` actually works for the export.

However in the notebook itself it is now also only displayed as the latex source 🙁 . Is there some priority list that IJulia uses to decide which `show` method to use?

edit: that frowning smiley looks way to angry…

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 14, 2020, 3:23pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/6 "2020-01-14T15:23:23Z")

</div>

IJulia should make all of these MIME types be sent for the object:

[https://github.com/JuliaLang/IJulia.jl/blob/bf0a53086545fe0f0695dc6e7c5ef35f047f4af5/src/display.jl#L6-L28](https://github.com/JuliaLang/IJulia.jl/blob/bf0a53086545fe0f0695dc6e7c5ef35f047f4af5/src/display.jl#L6-L28)

What determines what actually gets rendered and shown on screen, I am not sure.

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 14, 2020, 4:02pm UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/7 "2020-01-14T16:02:32Z")

</div>

After some research it seems like the priority list is hard coded and Latex takes precedence above images ([source](https://stackoverflow.com/questions/45560556/what-is-the-precedence-order-of-jupyter-rich-representations)).

Which would be fine if the mathjax engine used to display latex would be able to handle pgfplots.

---

<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:** [January 15, 2020, 8:31am UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/8 "2020-01-15T08:31:45Z")

</div>

> [@karlroyen](#):
>
> if the mathjax engine used to display latex would be able to handle pgfplots.

I am not quite sure you would want this, even if it was implemented.

While rendering math is now really fast with MathJaX (and even better with KaTeX), pgfplots is comparatively expensive, even with engines like LuaTeX, and uses a lot of TeX functionality and special backengs (via TikZ/pgf). One would essentially have to implement a rather complete TeX engine in the browser.

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 17, 2020, 8:16am UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/9 "2020-01-17T08:16:23Z")

</div>

Yes, you are probably right.

I’ll try to use a custom MIME type like `"text/pgf"` for the pgf source instead and use a custom template for `nbconvert`.

---

<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:** [January 17, 2020, 8:49am UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/10 "2020-01-17T08:49:38Z")

</div>

Let us know how it goes, and maybe submit a PR if it works. Output as `MIME"text/pgf"` should be simple to implement with a simple wrapper.

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 18, 2020, 10:54am UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/11 "2020-01-18T10:54:06Z")

</div>

I have gotten a first working version using the custom mime type “text/pgf” and a custom latex exporter.

I hadn’t had to change much on the Julia side except from adding the according `show` methods and registering the new mime type with IJulia.

But I had to add a custom exporter for nbconvert that prefers “text/pgf” over image mimes and adds the according pgf source to the files.

You can give it a try at [https://github.com/karlwessel/LatexWithPGFPlots](https://github.com/karlwessel/LatexWithPGFPlots)

Big parts missing are support for Plots.jl and a bit clean up of the generated files for the latex document.

---

<div class="post-metadata">

**Author:** ![karlroyen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karlroyen/32/9301_2.png) [@karlroyen](https://discourse.julialang.org/u/karlroyen)\
**Post date:** [January 20, 2020, 9:58am UTC](https://discourse.julialang.org/t/jupyter-notebook-use-pgf-code-instead-of-pdf-svg-image-for-export-to-latex/33344/12 "2020-01-20T09:58:00Z")

</div>

Last update: I added support for PGFPlots.jl and Plots.jl and added a direct “PDF via LaTeX with pgf”-exporter.

So [this](https://github.com/karlwessel/LatexWithPGFPlots) solves my question 🙂 .
