[Tex/LaTex] How to clean auxiliary files within subdirectories with latexmk

auxiliary-fileslatexmk

Well, the title is pretty much self-explanatory but for sake of clarity, I have latex paper with such structure:

.
├── back_matter
│   └── some_tex_files.tex
├── bibliography
│   └── bibliography.bib
├── body_matter
│   ├── a_file.tex
│   └── another_file.tex
├── graphics
│   └── some_images.png
├── styles
│   ├── bib_style.bbl
│   └── paper_style.cls
├── paper.tex
└── Makefile

And a Makefile like so:

.PHONY: paper.pdf

all: paper.pdf

paper.pdf: paper.tex
    latexmk -pdf -pdflatex="pdflatex -interactive=nonstopmode" -use-make paper.tex

clean:
    latexmk -CA

make clean successfully cleans all the auxiliary files in the root (.), however, it does not clean the auxiliary files created in the subdirectories. How to define a rule for cleaning all the .aux files within the subfolders? I should mention that I use \input to add the files to paper.tex.

P.S. Is this make file well written?

Best Answer

  1. If all the files in the subdirectories are input using \input, then there'll be no .aux files in the subdirectories. You will only get such .aux files if the running of pdflatex on some other file made them or if you used \include at some point in the past instead of \input. In any case, they are not part of the files involved in the current run of pdflatex, so latexmk correctly does not delete them when you do latexmk -CA. On the other hand, if a file is brought into the document by \include, then the corresponding .aux file is involved in the document, and latexmk -CA will delete it.
    If you find some other behavior, we'll need a MWE to understand the problem.

  2. As regards the Makefile, you should look at the section about this in the latexmk documentation. The relevant section "USING latexmk WITH make' is near the end of the documentation, and there are some options to latexmk that are useful. The important thing is that with a little care, make can find all the dependency information that latexmk computes.

  3. If your description of the project is complete, then a Makefile is unnecessary; the command latexmk without any arguments will do what you need, particularly if there is only a single .tex file in your root directory, and any configuration needed is in a latexmkrc file. A Makefile is normally needed only when there are other things you need it to do besides those that latexmk is designed to do.

For your case, a suitable Makefile is

DEPS_DIR = .deps
.PHONY: all clean

# Read in known dependencies:
$(foreach file,$(wildcard $(DEPS_DIR)/*P),$(eval -include $(file)))

all: paper.pdf

%.pdf: %.tex
    @if [ ! -e $(DEPS_DIR) ]; then mkdir $(DEPS_DIR); fi
    latexmk -deps-out=$(DEPS_DIR)/$@P -pdf -dvi- -ps- $<

clean:
    latexmk -C

This uses some special features of GNU make. Dependence information is kept in a subdirectory named .deps.