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

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.

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

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.

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

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…

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

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

After some research it seems like the priority list is hard coded and Latex takes precedence above images (source).

Which would be fine 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.

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.

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.

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

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

1 Like

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

So this solves my question .