[Tex/LaTex] How to prevent tikzexternalize to generate files in current directory

pdftextikz-externaltikz-pgf

I'm using pdflatex. I'm building in a "build" subdirectory with the –output-directory of pdflatex and it works really well, every file is generated in this directory.

Now, I want to externalize my pgfplots, so I'm using:

\usetikzlibrary{external}
\tikzexternalize[shell escape=-enable-write1 -shell-escape, prefix=./build/]

In that case, the external files are created in the build directory (the MD5 files are created in the build/build, but it does not matter). However, after this, several files are created in the working directory rather than in the build directory. After the build, I have these files:

master.acn
master.glo
master.ist
master.maf
master.mw
master.run.xml

in the working directory rather than in the build directory 🙁

I know that these files are not related to tikz, but they only appear here when I use tikzexternalize.

Why is tikzexternalize creating some extra files in the current working directory ? Is it possible to get rid of these files ?

Thanks

Best Answer

I think the most straightforward way to do this is not to set prefix at all but to instead do two things.

  1. Ensure that --output-directory is passed to the compiler when it is run by the externalisation process by overriding the default compilation command.

    • This should ensure that all files generated when producing the externalised images are written to build/ and not only those directly concerned with externalisation.
  2. Ensure that TikZ searches in the correct place for the externalised images by overriding the default image inclusion command.

    • This should ensure TeX looks in the correct place for the externalised images by directly manipulating the path used when including images rather than by setting prefix. prefix won't work here because it will also be used when externalising, creating an unwanted cumulative effect when producing images (build/build/<image>) but not when including them (build/<image>), resulting in errors.

For example, you might want something like this (based on the information in your question and the default explained on page 609 of the TikZ manual and the information on page 616):

\begin{filecontents}{\jobname.bib}
@article{author-art,
  author              =   {Author, A. N.},
  title               =   {Believe It or Not \dots},
  volume              =   290,
  year                =   3056,
  doi                 =   {44.897/gje.x789},
  pages               =   {x789},
  eprinttype          =   {gje},
  eprint              =   {x789},
  eprintclass         =   {290},
  journal             =   {Greatest Journal Ever}}
@article{author-anart,
  author              =   {Author, A. N.},
  title               =   {Suppose It or Not \dots},
  volume              =   78,
  number              =   3,
  year                =   3049,
  pages               =   {22--29},
  eprinttype          =   {jstor},
  eprint              =   {12980258},
  journal             =   {Second Best Journal Ever}}
\end{filecontents}

\documentclass[british]{article}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}
\usepackage[backend=biber]{biblatex}
\bibliography{\jobname}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{%
  external/system call={pdflatex \tikzexternalcheckshellescape --halt-on-error --interaction=batchmode --output-directory=./build --jobname "\image" "\texsource"},
  /pgf/images/include external/.code={%
    \includegraphics{build/#1}%
  },
}
\begin{document}
\Textcite{author-art,author-anart}

\begin{tikzpicture}
  \draw (0,0) rectangle (1,1);
\end{tikzpicture}

\printbibliography

\end{document}

I then compiled with

pdflatex --shell-escape --output-directory=./build <filename>.tex 
biber --output-directory=./build <filename>
pdflatex --shell-escape --output-directory=./build <filename>.tex 

This generated all files in build/ and seemed to compile successfully, using the externalised image rather than recompiling the TikZ code on the second run.

result