Create PDF document with PGFPlotsX figures, captions, a table, and some text

I’m using PGFPlotsX to create my figures, but I want to bundle them in a report with some metadata that comes with those figures (mostly some mean-error estimates in a table and comments in a paragraph of text). I want to generate many such reports (one per experimental run) so I need to automate this and would love for this to be platform-independent.

Is there any easy way to:

  1. create a .tex document that includes the PGFPlotsX-generated figures,
  2. give the figures captions perhaps,
  3. add a table,
  4. add a comments section/paragraph,
  5. and compile that .tex file into a PDF?

Thanks in advance!

I’ve just done this for my own purposes; the easiest way I could think of was to have a LaTeX template as a separate file that I did a simple find and replace on a selected set of identifiers. This combined with print_tex from PGFPlotsX.jl and Latexify.jl applied to a dataframe (or alternatively LaTeXTabulars.jl) did the job quite well. I didn’t automate the compiling though but that should be fairly easy.

Thanks for the input. I was afraid that would be the only option.

I have to say though, with the job PGFPlotsX does, I think it would be easy to just add some text after the tikzpicture, change the document type to article, and voila I have a report…? Maybe one of their developers has something to add…

Sure, if you don’t want to add that much (or even if you do) then you can wrap everything in a TikzDocument and push! strings/more plots into it.


!!! that looks great (just saw it now too)!!!

This really is a great solution, super easy to add a paragraph to the figures. Now if I could only change the document class to say article I could even get Section{}

You can use the latest master of pythontex with LaTeX, which will let you evaluate Julia code and use the result in the LaTeX document. I can send you an example if you’d like…

1 Like

I think that doing this outside PGFPlotsX is the best solution, as @dawbarton first suggested. Something as simple as having an A.tex and B.tex for stuff that come before and after, and then print_tex the plot in between, could work fine and allow you to customize things.

Or something like

Replaces `placeholder` in the template with `plot` in TeX format,
write output to `output_path`.
function plot_into_template(plot, template_path, output_path;
                            placeholder = "PLOT_HERE")
    template_tex = read(template_path, String)
    io = IOBuffer()
    print_tex(io, plot)
    plot_tex = String(take!(io))
    output_tex = replace(template_tex, placeholder => plot_tex)
    write(output_path, output_tex)

(untested) would allow you to edit a LaTeX file separately for the template.

1 Like

That’s great! Is there a way to compile that output_path into a PDF cross-platform?

Right now with your excellent package I’m doing this:

td = TikzDocument()
push!(td, TikzPicture(plots))
push!(td, errors)
push!(td, comments)
pgfsave("report.pdf", td)

and I have a finished PDF – yea it looks a bit raw, but a report nevertheless :slight_smile:

Eg call pdflatex, or something like latexmk if there are extra steps (cross-refs, bibliography, etc).

To build the PDF, why not take advantage of existing PGFPlotsX code - see particularly run_latex_once.

OK, I just looked at PGFPlotsX’s build process and I now realize that it’s not cross-platform, like I I’m not sure it’ll work on Windows for instance… Maybe I’m totally wrong and it will work on any OS? If not, wouldn’t it be better to make use of BinaryProvider to install a local “LaTeX engine” (like FFMPEG.jl gets ffmpeg installed)?

Yes! That would be the easiest: piggyback their hard work :wink:

But again, if PGFPlotsX only works on Linux then this also will only work on Linux.

I’m running it on Windows as I type!

1 Like

Alright then!
But to be clear, as to dependencies, if you hadn’t installed lualatex or pdflatex, pdftoppm, pdf2svg then it wouldn’t.

Unfortunately Latex is a rather large dependency and I doubt that it would ever be sensible to put on BinaryProvider (my current latex install is around 4GB). This is why TeXLive was created - there isn’t much point redoing all their (considerable) effort.


Yes, there are the extra dependencies on top of Latex but they are only required if Juypter or Juno are used. (I don’t think I have them installed on my current Windows machine…)

Just as “they” did with Python (and conda if I’m not mistaken), maybe there’s a way to install a contained LaTeX engine if a local one does not exist. One could assume that the user is ok with a contained dependency, even if it’s huge. My reasoning being: if a user wants pgfplots-functionalities then s/he is ok with installing all the required dependencies, even if they are 4 GB, the size doesn’t really matter (to a certain point). PGFPlotsX would just give them that option in a more convenient way (than manually installing LaTeX on their machine).

At the end of the day PGFPlotsX is a wrapper for a bunch of stuff that happens to be 4 GB (or whatever it ends up being). You either have or don’t have the required dependencies. One nice option might be to automatically fetch any missing dependencies.

You can do this, but note that this is internal code and can change at any point without prior notice.

The implicit assumption is that most PGFPlotsX users use LaTeX in some other way, so they already have it installed.

I would guess that 4GB is a bit too much to casually download/duplicate at this point.


The size of the latex installation depends very much on how many packages you install. You really want the latex installation to be global to your system so you can use the same packages when using PGFPlotsX and what you end up using in publications later. Also, trying to bundle latex would be an extreme extreme headache. So the bottomline is, if you want to use PGFPlotsX you need to install latex on your system yourself.

All of this makes sense, for sure.

The reason why I brought this up is that I’m building analysis tools for people that might not use LaTeX, and ideally, they’d be able to run these tools locally on their machines. But then I’d need for them to go and install LaTeX to use these tools because I’m relying on the excellent pgfplots library.

This is all moot though, I can imagine that bundling LaTeX is indeed an enormous headache and too much work for the gain. Dropping this now, thanks a lot for all the feedback!