[Tex/LaTex] How to build a Makefile that builds a LaTeX document with dependencies in one run

latexmkluatexmakefile

I am trying to create a Makefile for my paper where I can type just make and it will compile everything in one go. The paper has several figures which are generated from scripts. However, everything I've tried requires several runs of make to get both the scripts to run and the paper itself.

I started with something based on this answer

all: paper.pdf

paper.pdf:

%.pdf: %.tex
    $(LATEXMK) -lualatex -halt-on-error -pdf -M -MP -MF $*.d $*

some-plot.pgf:
    script_to_generate_plot.py

-include *.d

However, from a clean build, or whenever I add a plot, this requires running make twice, once to generate the .d file and once to actually build the paper. And annoyingly the first time you just have to bypass an annoying "file not found" prompt.

I then noticed that latexmk has a flag to use make to build missing files, so I'm trying that:

all: paper.pdf

paper.pdf:

%.pdf: %.tex
    $(LATEXMK) -lualatex -interaction=nonstopmode -pdf -dvi- -ps- -use-make $*

some-plot.pgf:
    python script_to_generate_plot.py

Now what this does is finds one plot missing, build it, then fails with

Latexmk: 'pdflatex': source file 'some-plot.pgf' doesn't exist. I'll try making it...
------------
Running 'make "some-plot.pgf"'
------------
python script_to_generate_plot.py
Latexmk: Summary of warnings:
  Latex failed to resolve 1 reference(s)
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 256
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs of latex/pdflatex.
make: *** [paper.pdf] Error 12

If I run make again that plot was built, but it does the same thing with another one.

If I keep running make it eventually builds all my plots one by one (I have several plots, some of which take a while to generate so I'd like to keep the scripts to generate them separate).

How can I make it so that it builds everything in one go, regardless of which and how many files need to be regenerated?

EDIT I guess I forgot to mention that my other goal here is to avoid manually stating every .tex/.pgf/.pdf file dependency in my Makefile. If I do that, there's no point to using latexmk. I've done the manual Makefile before, but I'm always forgetting to update the dependency graph whenever I add a new file or rearrange things. My understanding is that the strength of latexmk is that it can figure that stuff out automatically. If I can get something that does that, that would be ideal. That and latexmk automatically rebuilds the paper and bib files as many times as necessary.

EDIT 2
I tried using the suggestion from the output, to use the -f flag to latexmk. This builds about 5 or so plots, then errors with

Rule 'pdflatex': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'origen-meeseeks.pgf'
Latexmk: Maximum runs of pdflatex reached without getting stable files
Failure to make 'paper.pdf'
Latexmk: Errors, in force_mode: so I tried finishing targets
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 256
Latexmk: Did not finish processing file 'paper':
   'pdflatex' needed too many passes
make: *** [paper.pdf] Error 12
transmutagen-papermaster*=$latexmk -

So it almost does what I want. How do I increase the max number of passes?

Best Answer

As I noted in my edit, if you add the -f flag to latexmk, it continues after each plot is generated and tries again.

Unfortunately, these "re-runs" are counted as part of the number of times latexmk calls latex to build the document. By default, it only does 5 of these, but you can modify this with a configuration parameter. Create a file .latexmkrc in the directory with your tex files and add

$max_repeat = 10

(replace 10 with some number bigger than the number of plots you have, probably at least 5 more than that).

I personally would consider these to be bugs in latexmk (building more than a single thing with make shouldn't require -f, each call to make shouldn't count against the max_repeat).

You also do need to add the file dependencies manually in the Makefile

paper.pdf: some-plot.pgf

I don't know if there's a way to make the -MF stuff from latexmk work properly with just one Make run that both generates and uses them.

Related Question