dionhaefner / pgfcache

LaTeX package for caching of PGF figures created with Matplotlib, just like tikz-externalize
MIT License
12 stars 1 forks source link

Support for output directory #2

Closed juupje closed 1 year ago

juupje commented 1 year ago

First of all, thanks for this package! It'll hopefully save me a lot of compiling time.

I've noticed that an error is thrown when pdflatex is run with the argument -output-directory. Specifying this is quite useful for me, since I hate having half a million .aux and .synctex.gz files in my workspace.

The issue is caused by the fact that pgfcache runs pdflatex with the expectation that the _pgfcache folder is in the working directory, whereas the \@writetempfile command seems to write into the output directory. Thus, there is no .tex file in the _pgfcache folder in the working directory, causing pdflatex to crash and no figure to be produced, which then causes the 'main' pdflatex command to throw an error, too. It would be great if this could be solved.

My solution

I've hacked together a solution by simply adding the line \edef\@outputdir{} below the definition of \@figdir and creating the command \setoutputdir as follows:

\newcommand{\setoutputdir}[1]{%
    \edef\@outputdir{#1/}
}

which overwrites the outputdir (which is an empty string by default). Finally, the pdflatex call is changed to

\immediate\write18{pdflatex -output-directory="\@outputdir\@figdir" \@outputdir\@figdir/\@figfile.tex}%

such that it takes the output directory into account. I can then specify the output directory in the preamble of my master file and everything works as expected (as long as the output-directory of pdflatex is the same of couse).

Obviously, I'm not very experienced with programming LaTeX, so I bet there is a more elegant way of doing this. I'd be happy to make a pull request if this solution if good enough :)

dionhaefner commented 1 year ago

Thanks for the thorough report. I can't think of a better solution since it seems to be impossible to get the value of the current output dir from within LaTeX. If you want to put a PR together I'd be happy to merge it.