I've been looking into using latexmk to create my documents because it automatically handles how many passes the document needs to be fully processed etc. So, for example, a simple run of latexmk -pdf file.tex will run pdflatex and bibtex as many times as necessary to resolve all references.

I was wondering whether it's possible to not lose the ability to do so when using the TikZ/external library in the list and make mode. If list and make is used, the TikZ/external makefile is generated, but it is not automatically called by latexmk. You can run the makefile manually and, of course, that will generate the graphic files, but the problem is that, as far as latexmk is concerned, the .tex files remain unchanged, so running latexmk once more does not insert the newly-generated graphics into the document.

So, given file.tex that uses TikZ/external in the list and make mode, this is what happens:

  • latexmk -pdf file.tex generates the pdf and inserts placeholders for the not-yet-generated graphics.
  • make -f file.makefile processes and generates the pdf files for the graphics.
  • latexmk -pdf file.tex does nothing because the source files have not changed.

I assume there's a way to force the generation of the pdf in the second latexmk pass, but that would mean that this would happen even if the source files remained unchanged.

I was wondering whether there was a way to use latexmk to handle all of this "automagicaly"? That would be great!

Here's a MWE to get everyone started:


\tikzexternalize[mode=list and make]

    \addplot coordinates {(1,1) (2,2) (3,3)};


This isn't latexmk automagic, but it avoids recompiling if the source .tex file hasn't changed:

latexmk -pdf file.tex
make -n -f file.makefile > /dev/null | grep pdflatex > /dev/null
if [ $? -eq 0 ] ; then
  make -f file.makefile
  latexmk -g -pdf file.tex

If the auto-generated makefile isn't actually going to compile any figures, the string "pdflatex" won't appear in the output of the dry-run; otherwise, the figures are compiled and latexmk is forced to do at least one more compilation.

Alternatively, here is a latexmkrc file that works most of the time: it runs the external makefile once per latexmk invocation, after the first time pdflatex is called.

If you are compiling the document from scratch, latexmk will most likely run pdflatex multiple times, and so the figures will be included after the first run. If you are not building from scratch, then there is a good chance that latexmk will recognize that the external figures are being included, see that they have been updated by the external makefile, and then decide to run pdflatex again because of the changing dependencies.

Warning: this is the first time I've written perl:

our %externalflag = ();

$pdflatex = 'internal mypdflatex %O %S %B';

sub mypdflatex {
  our %externalflag;
  my $n = scalar(@_);
  my @args = @_[0 .. $n - 2];
  my $base = $_[$n - 1];

  system 'pdflatex', @args;
  if ($? != 0) {
    return $?
  if ( !defined $externalflag->{$base} ) {
    $externalflag->{$base} = 1;
    system ("$make -j8 -f $base.makefile");
  return $?;
  • Interesting. I didn't think of using this sort of sorcery to solve this. It seems like this method does not sacrifice any functionality provided by the list and make mode. Thanks a lot! I won't mark this as accepted just yet in case a more portable, LaTeX-y, latexmk-y answer is provided. – sudosensei Nov 20 '13 at 19:42
  • I very much like the first solution you present. I have to use a slightly modified version where I leave out the first > /dev/null – Octaviour Nov 21 '17 at 12:33
  • For reference only, John Collins improved and corrected (his words) this solution in another question. – Leone Mar 9 at 7:09

This is not a straight forward answer to your question, but another way of achieving your overall goal: Outsourcing TikZ-pictures and rebuilding them only if they have changed.

This can be done using the standalone class:


    \addplot coordinates {(1,1) (2,2) (3,3)};


This could fit your needs quite well, as you are already putting your TikZ-pictures in separate files and everything works "automagically" with latexmk. Caveat: The files have to have the extension .tex instead of .tikz and you have to add a few more lines of code.

  • 1
    This is a good alternative, but it sacrifices some of the functionality offered by the list and make mode unfortunately. For example, the ability to automatically re-generate the plot when a data file changes. I'm also not sure whether the standalone method can resolve a reference to, for example, a pgfplots legend (which I think requires two passes). – sudosensei Nov 20 '13 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.