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

**URL:** <https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478>\
**Category:** Visualization\
**Tags:** question, latex, pgfplotsx\
**Created:** [July 17, 2019, 7:00pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478 "2019-07-17T19:00:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 17, 2019, 7:00pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/1 "2019-07-17T19:00:13Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 17, 2019, 7:27pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/2 "2019-07-17T19:27:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 17, 2019, 7:31pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/3 "2019-07-17T19:31:39Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 17, 2019, 7:52pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/4 "2019-07-17T19:52:22Z")

</div>

Sure, if you don’t want to add that much (or even if you do) then you can wrap everything in a [`TikzDocument`](https://kristofferc.github.io/PGFPlotsX.jl/stable/man/picdoc/#TikzDocument-1) and `push!` strings/more plots into it.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 17, 2019, 7:53pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/5 "2019-07-17T19:53:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 17, 2019, 10:39pm UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/6 "2019-07-17T22:39:12Z")

</div>

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{}

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [July 18, 2019, 12:54am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/7 "2019-07-18T00:54:56Z")

</div>

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…

---

<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:** [July 18, 2019, 5:48am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/8 "2019-07-18T05:48:36Z")

</div>

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

```julia
"""
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)
end

```

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

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 18, 2019, 8:14am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/9 "2019-07-18T08:14:31Z")

</div>

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:

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

---

<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:** [July 18, 2019, 8:29am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/10 "2019-07-18T08:29:24Z")

</div>

> [@yakir12](#):
>
> Is there a way to compile that `output_path` into a PDF cross-platform?

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

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 18, 2019, 8:39am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/11 "2019-07-18T08:39:07Z")

</div>

To build the PDF, why not take advantage of existing PGFPlotsX code - see [https://github.com/KristofferC/PGFPlotsX.jl/blob/master/src/build.jl](https://github.com/KristofferC/PGFPlotsX.jl/blob/master/src/build.jl) particularly `run_latex_once`.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 18, 2019, 8:46am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/12 "2019-07-18T08:46:17Z")

</div>

> [@Tamas\_Papp](#):
>
> Eg call `pdflatex` , or something like `latexmk` if there are extra steps (cross-refs, bibliography, etc).

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](https://github.com/JuliaIO/FFMPEG.jl/blob/master/deps/build.jl))?

> [@dawbarton](#):
>
> To build the PDF, why not take advantage of existing PGFPlotsX code - see [PGFPlotsX.jl/src/build.jl at master · KristofferC/PGFPlotsX.jl · GitHub](https://github.com/KristofferC/PGFPlotsX.jl/blob/master/src/build.jl) particularly `run_latex_once` .

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

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

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 18, 2019, 8:47am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/13 "2019-07-18T08:47:16Z")

</div>

I’m running it on Windows as I type!

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 18, 2019, 8:48am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/14 "2019-07-18T08:48:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 18, 2019, 8:49am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/15 "2019-07-18T08:49:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 18, 2019, 8:50am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/16 "2019-07-18T08:50:49Z")

</div>

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…)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 18, 2019, 9:00am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/17 "2019-07-18T09:00:49Z")

</div>

> [@dawbarton](#):
>
> Latex is a rather large dependency

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.

---

<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:** [July 18, 2019, 9:20am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/18 "2019-07-18T09:20:31Z")

</div>

> [@dawbarton](#):
>
> particularly `run_latex_once`

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

> [@yakir12](#):
>
> 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).

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.

---

<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:** [July 18, 2019, 9:47am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/19 "2019-07-18T09:47:16Z")

</div>

> [@yakir12](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [July 18, 2019, 9:55am UTC](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478/20 "2019-07-18T09:55:19Z")

</div>

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!

[Next page](https://discourse.julialang.org/t/create-pdf-document-with-pgfplotsx-figures-captions-a-table-and-some-text/26478.md?page=2)
