Skip to content

Instantly share code, notes, and snippets.

@markusritschel
Last active July 26, 2019 19:21
Show Gist options
  • Save markusritschel/e28c911c41ca17786fbedbc750b971bd to your computer and use it in GitHub Desktop.
Save markusritschel/e28c911c41ca17786fbedbc750b971bd to your computer and use it in GitHub Desktop.
Makefile for LaTeX compilation via XeTeX and some commands for image processing
#!/usr/bin/make -f
SHELL = /bin/sh
# The root directory for all subdirectories
srcdir = .
# The directory where the images are
imagedir = ${srcdir}/images
doc_name = 'thesis'
quality = 'ebook' # possible options: screen, ebook, printer, prepress
.PHONY: compile
compile:
xelatex -synctex=1 -interaction=nonstopmode -file-line-error -shell-escape main.tex; \
biber main; \
xelatex -synctex=1 -interaction=nonstopmode -file-line-error -shell-escape main.tex; \
xelatex -synctex=1 -interaction=nonstopmode -file-line-error -shell-escape main.tex;
.PHONY: cleanup
cleanup:
@echo delete auxiliary files
@for ext in aux bbl bcf blg lof log_ lot out run.xml synctex.gz tdo toc; \
do \
find . -name \*.$${ext} -type f -delete; \
done; \
find . \( -name ".DS_Store" -o -name "Thumbs.db" \) -type f -delete; \
find . -name _minted\* -type d -exec rm -rf {} \;
.PHONY: clear_logs
clear_logs:
@echo delete log files
@find . -name \*.log -type f -delete
.PHONY: latex
latex: compile cleanup
mv ./main.pdf ${doc_name}.pdf
.PHONY: compress
compress:
gs \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pdfwrite \
-dPrinted=false \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/${quality} \
-dDownsampleColorImages=true \
-dColorImageResolution=150 \
-sOutputFile=${doc_name}_compressed.pdf \
${doc_name}.pdf
# possible options: screen, ebook, printer, prepress
# -dDownsampleColorImages=true -dColorImageResolution=150
# Alternatively: instead of `gs` use `ps2pdf`
# https://www.digitalocean.com/community/tutorials/how-to-use-makefiles-to-automate-repetitive-tasks-on-an-ubuntu-vps
.SUFFIXES: .jpg .jpeg .png .pdf
JPEG=$(wildcard ${imagedir}/*.jpg ${imagedir}/*.jpeg) # find all JPG and JPEG files
JPG=$(JPEG:.jpeg=.jpg) # internally rename all JPEG to JPG
PDF=$(wildcard ${imagedir}/*.pdf) # find all PDF files
jpg2png=$(JPG:.jpg=.png) # naming rule for JPG to PNG conversion
pdf2png=$(PDF:.pdf=.png) # naming rule for PDF to PNG conversion
.PHONY: clear_images
clear_images:
@echo remove all PNG files in ${imagedir} which have a PDF or JPG parent
@rm -f ${pdf2png}
@rm -f ${jpg2png}
.PHONY: convert
convert: $(jpg2png) $(pdf2png)
@echo ---
@echo finished conversion
# rule for converting JPG and PDF to PNG
# compare compression result and delete PNG if result is bad
.jpeg.png .jpg.png .pdf.png:
@convert $< -resize 800x800 -quality 85% $@
@echo converted $<
@COMP=$$((`stat -c%s "$<"`/`stat -c%s "$@"`)); [ $$COMP -lt 2 ] && echo "\tbad compression: remove $@" && rm $@ || true
.PHONY: figures
# clears all PNGs with a JPG or PDF parent, crops PDF files, converts to PNG, and crops PNGs
figures: clear_images crop_pdf convert crop_png
@echo ---
@echo finished creating figures
.PHONY: crop_pdf
crop_pdf:
@echo
@echo crop all pdf files in ${imagedir}
@for file in `find ${imagedir}/ -iname "*.pdf" -type f`; \
do \
pdfcrop $${file} $${file}; \
done;
.PHONY: crop_png
crop_png:
@echo
@echo crop all png files in ${imagedir}
@for file in `find ${imagedir}/ -iname "*.png" -type f`; \
do \
echo crop $${file}; \
convert $${file} -trim $${file}; \
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment