showyourwork / showyourwork

A workflow for reproducible and open scientific articles

Home Page:https://show-your.work

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for matplotlib's native pgf backend

HealthyPear opened this issue · comments

I tried naïvely to test the generation of both PGF files and PDF files generated directly by the homonym backend of matplotlib.

This has the advantage that the plot is done by LaTeX, and not by matplotlib and only then imported as an image (binary file).
More details here, https://matplotlib.org/stable/tutorials/text/pgf.html#choosing-the-tex-system.

In my case, the showyourwork build command fails with,

File ms.pdf is present on disk, but there is no valid rule to generate it.
MissingInputException: Missing input files for rule syw__compile:
    output: ms.pdf, ms.synctex.gz, src/tex/xstring.sty, src/tex/showyourwork-logo.pdf, src/tex/showyourwork.otf, src/tex/xstring.tex, src/tex/showyourwork-stamp.pdf, src/tex/listofitems.tex, src/tex/lineno.sty, src/tex/showyourwork.tex, src/tex/showyourwork-metadata.tex, /Users/michele/Documents/Programming/showyourwork/article/.showyourwork/compile
    affected files:
        src/tex/figures/3D.pdf
MissingInputException: Missing input files for rule syw__arxiv:
    output: arxiv.tar.gz, ms.pdf, src/tex/xstring.sty, src/tex/showyourwork-logo.pdf, src/tex/showyourwork.otf, src/tex/xstring.tex, src/tex/showyourwork-stamp.pdf, src/tex/listofitems.tex, src/tex/lineno.sty, src/tex/showyourwork.tex, src/tex/showyourwork-metadata.tex, /Users/michele/Documents/Programming/showyourwork/article/.showyourwork/compile
    affected files:
        src/tex/figures/3D.pdf

I attach both available logs (the tectonic one didn't get generated as the build fails at figure generation)

showyourwork.log
snakemake.log

It seems that a dependency is missing, but I don't get which one (it's only matplotlib) - perhaps is the TeX system (which is usually either of xelatex (default), lualatex and pdflatex with the first 2 usually recommended)?

The script that generates the plot is a simple 3D plot example from matplotlib's gallery plus the modifications required to make it work with the repository structure and the PGF backend, in general something like the following,

import paths

import matplotlib.pyplot as plt
import numpy as np

# PLOT CODE HERE

fig.savefig(paths.figures / "matplotlib_pgf.pdf", backend="pgf")

Maybe this matplotlib issue is the cause of this...

matplotlib/matplotlib#22715

Here it's failing because no figure called "3D.pdf" is being created. In your sample code, you're saving it with a different name, but are you sure that in your actual code you're saving it with the right name?

Sorry that was an old test because I thought maybe the name of the file should be the same as the name of the script.

No, before I tried saving

fig.savefig(paths.figures / "3D.pdf", backend="pgf", bbox_inches="tight", dpi=300)

and requiring

\begin{figure}
    \centering
    \includegraphics[width=\linewidth, keepaspectratio]{figures/3D.pdf}
    \caption{A 3D plot made with matplotlib pgf backend.}
\end{figure}

and it failed as shown above (the logs refer to the original try, in fact you can see

affected files:
        src/tex/figures/3D.pdf

You need a \script command in the figure environment! Otherwise showyourwork doesn't know the data dependencies.

Oh, my bad rookie mistake!

It works perfectly, you can close this issue.

🚀🚀🎉

I even tested the use of PGF files (so the PDF is produced at compiled time by pgfplots) and that also works.

And also the use of a different TeX system, e.g.

plt.rcParams.update(
    {
        "pgf.texsystem": "lualatex",
    }
)

this is a bit less straightforward for me...I thought tectonic was a different distribution with no direct support for pdflatex, xelatex and lualatex.