1# ============================================================================
2# Makefile for LaTeX Compilation
3# ============================================================================
4# This Makefile automates the compilation of LaTeX documents to PDF.
5#
6# Usage:
7# make - Compile hello.tex to hello.pdf
8# make clean - Remove all generated files
9# make view - Open the PDF (macOS/Linux)
10# ============================================================================
11
12# Variables
13LATEX = pdflatex
14MAIN = hello
15SOURCE = $(MAIN).tex
16OUTPUT = $(MAIN).pdf
17
18# Auxiliary files created during compilation
19AUX_FILES = *.aux *.log *.out *.toc *.lof *.lot *.fls *.fdb_latexmk *.synctex.gz
20
21# Default target
22all: $(OUTPUT)
23
24# Compile the LaTeX document
25# Run twice to resolve cross-references and TOC
26$(OUTPUT): $(SOURCE)
27 @echo "Compiling $(SOURCE)..."
28 $(LATEX) $(SOURCE)
29 @echo "Running second pass for cross-references..."
30 $(LATEX) $(SOURCE)
31 @echo "Compilation complete: $(OUTPUT)"
32
33# Clean auxiliary files
34clean:
35 @echo "Cleaning auxiliary files..."
36 rm -f $(AUX_FILES)
37 @echo "Clean complete."
38
39# Clean everything including PDF
40cleanall: clean
41 @echo "Removing PDF output..."
42 rm -f $(OUTPUT)
43 @echo "All files removed."
44
45# Open PDF viewer (platform-specific)
46view: $(OUTPUT)
47ifeq ($(shell uname), Darwin)
48 @open $(OUTPUT)
49else ifeq ($(shell uname), Linux)
50 @xdg-open $(OUTPUT) 2>/dev/null || evince $(OUTPUT) 2>/dev/null
51else
52 @echo "Please open $(OUTPUT) manually."
53endif
54
55# Force recompilation
56rebuild: cleanall all
57
58# Help message
59help:
60 @echo "Available targets:"
61 @echo " make - Compile LaTeX document"
62 @echo " make clean - Remove auxiliary files"
63 @echo " make cleanall - Remove all generated files including PDF"
64 @echo " make view - Open PDF in viewer"
65 @echo " make rebuild - Clean and recompile"
66
67# Phony targets (not actual files)
68.PHONY: all clean cleanall view rebuild help
69
70# ============================================================================
71# Notes:
72# - pdflatex is run twice because cross-references, TOC, etc. need two passes
73# - For documents with bibliography or complex references, you may need:
74# pdflatex -> bibtex -> pdflatex -> pdflatex
75# - latexmk is a more advanced tool that handles this automatically
76# ============================================================================