Search code examples
makefilelatexcropgnu

iterate over pdf files with pdfcrop in a makefile and assign dependency


I am able to iterate over pdf files (pictures) in my makefile to crop the margins. For this I am using the pdfcrop function of TexLive on Windows 10. But now I am not able to create a dependency. If a pdf file is not cropped or there is a new pdf file that is not cropped in my picture folder, pdfcrop shall crop the margins and save it with the same name in the same folder (so actually override the old pdf file). I think this might be easy, but I just can't figure out how to do this. This is my Code in the makefile.

MAIN_FILE = Dissertation
FIG_DIR  = ./Bilder/Abbildungen
FIG_FILES  := $(wildcard $(FIG_DIR)/*.pdf)

all: $(MAIN_FILE).pdf

$(MAIN_FILE).pdf: $(MAIN_FILE).tex $(CROP_FILES)
    pdflatex $(MAIN_FILE).tex

CROP_FILES = ${FIG_FILES:%=%.crop}

$(CROP_FILES): $(FIG_FILES) # this line doesn't seem work correctly
    $(foreach FIG_FILE, $(FIG_FILES), $(call CROP, $(FIG_FILE)))

define CROP
    pdfcrop $(1) $(1).crop

endef

Solution

  • Alternative solution, which will (hopefully) check timestamp for each PDF file, and activate CROP only on updated/new files.

    The cropped file is left in the same file name. If the timestamp of the '.crop' file is newer that the 'pdf' file, it means file is cropped, otherwise, PDF file will be cropped, and the '.crop' file will be touched.

    MAIN_FILE = Dissertation
    FIG_DIR  = ./Bilder/Abbildungen
    FIG_FILES  := $(wildcard $(FIG_DIR)/*.pdf)
    
    all: $(MAIN_FILE).pdf
    
        # The '.crop' timestamp capture the time the file was cropped
    CROP_FILES=${FIG_FILES:%=%.crop}
    
    %.crop: %
            pdfcrop $< $<
            touch $@
    
    $(MAIN_FILE).pdf: $(MAIN_FILE).tex $(CROP_FILES)
            pdflatex $(MAIN_FILE).tex
    
    

    Modification to place 'crop' files to timestamp folder

    TS_DIR=ts
    CROP_FILES = ${FIG_FILES:${FIG_DIR}/%=${TS_DIR}/%.crop}
    
    ${TS_DIR}/%.crop: ${FIG_DIR}/%
            @mkdir -p ${TS_DIR}
            pdfcrop $< $<
            touch $@
    

    Suggesting more testing, as logic is more complex. The '%.crop' target is replaced with a rule to generate them at ${TS_DIR}