I am using latexmk to compile my document, and have written a custom dependency rule to generate some files to include. The processing involves creating some intermediate files which I'd like to clean with -c. However, these intermediate files are only removed if I set $cleanup_includes_generated = 1; which results in the output PDF being deleted also.

For a minimal working example, use the following latexmkrc file which simply copies a file to an intermediate and then final destination. It uses the rdb_add_generated() routine mentioned by John Collins in this answer about cleaning custom dependencies.

$pdf_mode = 1;
$cleanup_includes_cusdep_generated = 1;

add_cus_dep('txt', 'inc', 0, 'test_dep');
sub test_dep {
    system("cp $_[0].txt $_[0].tmp");
    system("cp $_[0].tmp $_[0].inc");

Use a simple TeX document main.tex:


And an even simpler file message.txt to process:

Hello world!

Running latexmk -interaction=nonstopmode creates a file database main.fdb_latexmk which includes the message.tmp intermediate file. However, latexmk -c and latexmk -C only remove the message.inc file and leave the intermediate one.

Changing $cleanup_includes_cusdep_generated = 1; to $cleanup_includes_generated = 1; in the latexmk config results in the intermediate file being deleted. However, this results in latexmk -c behaving the same as latexmk -C and removing the output PDF, which is not ideal.

I suspect rdb_add_generated() is adding the intermediate file to the general list of generated files rather than a list of files generated by custom dependencies. Is there some way to mark it as a custom generated file, or am I stuck with choosing between leaving the intermediate files uncleaned or deleting all generated files?

  • This doesn't directly answer the question, but personally I would do something like this using make, and call latexmk from make to do the things that make can't do - in particular, automatically re-running LaTeX enough times for cross references etc to stabilize.
    – alephzero
    Dec 30 '18 at 10:29

This behavior is actually due to a couple of bugs in latexmk. (a) If you specify $cleanup_includes_cusdep_generated = 1;, then latexmk fails to delete the extra generated file that you specified. (b) If you specify $cleanup_includes_generated = 1;, then latexmk deletes files that it shouldn't (e.g., a .pdf file) when you use latexmk -c.

I've corrected this for the next release of latexmk.

  • Thanks! Just to clarify (b), did you mean $cleanup_includes_generated = 1;? With $cleanup_includes_cusdep_generated = 1; latexmk leaves the PDF for me.
    – Blair
    Dec 31 '18 at 21:57
  • 1
    Yes. I've corrected the answer. Jan 1 '19 at 1:29

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.