Skip to content

Instantly share code, notes, and snippets.

@nouredd2
Last active October 21, 2025 17:29
Show Gist options
  • Save nouredd2/707bf26c6107f05465873d346179cdec to your computer and use it in GitHub Desktop.
Save nouredd2/707bf26c6107f05465873d346179cdec to your computer and use it in GitHub Desktop.
Generic makefile for latex project using latexmk
# "Improved" Makefile for LaTeX documents
# INTPUFILE This defines the name of the file you want to latex
# the make file will automatically look for INPUT.tex as the latex file
# check if main file name is in .latexmain, if not, ask for it and save it.
MAINFILE := $(shell if [ -f .latexmain ]; then cat .latexmain; else read -p "Enter main tex file name: " name; echo $$name > .latexmain; cat .latexmain; fi)
INPUTFILE := $(shell basename -s .tex $(MAINFILE))
OTHERFILES := $(shell find . -name '*.tex' -not -name $(MAINFILE))
CLASS_FILES := $(wildcard *.cls)
STYLE_FILES := $(wildcard *.sty)
LMK_FLAGS := -output-directory=./build/ -pdf -pdflatex="pdflatex -synctex=1"
POUND := \#
ifeq ($(OS),Windows_NT)
detected_OS := Windows
else
detected_OS := $(shell uname)
endif
ifeq ($(detected_OS),Windows)
OPEN_APP := start
else
ifeq ($(detected_OS),Linux)
OPEN_APP := xdg-open
else
OPEN_APP := open
endif
endif
# define standard colors
ifneq (,$(findstring xterm,${TERM}))
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
LIGHTPURPLE := $(shell tput -Txterm setaf 4)
PURPLE := $(shell tput -Txterm setaf 5)
BLUE := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
else
BLACK := ""
RED := ""
GREEN := ""
YELLOW := ""
LIGHTPURPLE := ""
PURPLE := ""
BLUE := ""
WHITE := ""
RESET := ""
endif
TARGET_COLOR := ${BLUE}
DESCRIPTION_COLOR := ${GREEN}
.PHONY: clean cleaner watch all view echo help
all: build/${INPUTFILE}.pdf ## Build all targets into the build directory.
build/${INPUTFILE}.pdf: ${INPUTFILE}.tex ${OTHERFILES} ${CLASS_FILES} ${STYLE_FILES} ## Build the target using latexmk.
latexmk ${LMK_FLAGS} $<
@echo '****************************************************************'
@echo '******** Did you spell-check? ********'
@echo '****************************************************************'
clean: ## Remove build directory and any temporary files.
latexmk -output-directory=./build/ -c
rm -rf *.bbl ./build/ *.bak
cleaner: ## Remove all files generated by the LaTeX compiler (more than clean).
latexmk -output-directory=./build/ -C
rm -rf *.bbl ./build/ *.bak
view: build/${INPUTFILE}.pdf ## View the output of the document using the default viewing app.
${OPEN_APP} $<
watch: LMK_FLAGS += -pvc -quiet
watch: build/${INPUTFILE}.pdf ## Watch the changes in the file and recompile on save.
@echo ${LMK_FLAGS}
count:
texcount -brief *.tex
help: ## Print this message
@echo ""
@echo " :: ${RED}Makefile to compile LaTex documents using latexmk${RESET} ::"
@echo ""
@echo "-----------------------------------------------------------------"
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "${TARGET_COLOR}%-30s${RESET} ${DESCRIPTION_COLOR}%s${RESET}\n", $$1, $$2}'
echo: ## Print debug information for dependencies
@echo ""
@echo "-----------------------------------------------------------------"
@echo " Input file: ${INPUTFILE}"
@echo " Other dependencies (.tex): ${OTHERFILES}"
@echo " Class dependencies (.cls): ${CLASS_FILES}"
@echo " Style dependencies (.sty): ${STYLE_FILES}"
@echo "-----------------------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment