在 makefile 中使用 pdfcrop 遍历 pdf 文件并分配依赖项

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

我可以在我的 makefile 中遍历 pdf 文件(图片)来裁剪边距。为此,我在 Windows 10 上使用 TexLive 的 pdfcrop 函数。但现在我无法创建依赖项。如果未裁剪 pdf 文件或在我的图片文件夹中有未裁剪的新 pdf 文件,pdfcrop 将裁剪边距并将其以相同的名称保存在同一文件夹中(因此实际上覆盖了旧的 pdf 文件)。我认为这可能很容易,但我只是不知道该怎么做。这是我在 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

如果您以与原始 PDF 文件相同的名称存储裁剪文件,Make 将无法判断特定文件是否被裁剪。或者,考虑以不同方式命名裁剪后的文件(例如,filename.pdf.crop)

    # Add '.crop' suffix to the original file name
CROP_FILES = ${FIG_FILES:%=%.crop}

    # Recreate ALL CROPPED files, on changes to ANY source file
$(CROP_FILES): $(FIG_FILES) # this line doesn't seem work correctly
    $(foreach FIG_FILE, $(FIG_FILES), $(call CROP, $(FIG_FILE)))

    # Based on doc, 2nd parameter to pdfcrop name the output file.
define CROP
    pdfcrop $(1) $(1).crop

endef

请注意,此版本将在添加、重新创建单个 PDF 文件时重新裁剪所有 PDF 文件。如果这是一个耗时的操作,您可以扩展 Makefile 来执行每个检查,并提高性能。

替代解决方案,它将(希望)检查每个 PDF 文件的时间戳,并仅在 updated/new 个文件上激活 CROP。

裁剪后的文件保留相同的文件名。如果 '.crop' 文件的时间戳比 'pdf' 文件新,则表示文件被裁剪,否则,PDF 文件将被裁剪,并触及 '.crop' 文件。

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

修改以将 'crop' 个文件放置到时间戳文件夹

TS_DIR=ts
CROP_FILES = ${FIG_FILES:${FIG_DIR}/%=${TS_DIR}/%.crop}

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

建议进行更多测试,因为逻辑更复杂。 '%.crop' 目标被替换为在 ${TS_DIR}

处生成它们的规则