Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
281 views
ubuntu2404
# Computational Physics LaTeX Template Makefile
# Optimized for PythonTeX compilation with shell-escape
#
# Keywords: computational physics latex makefile, pythontex compilation,
# quantum mechanics latex build, monte carlo latex template compilation

# LaTeX compiler settings
LATEX = pdflatex
MAIN = main
BUILD_DIR = build

# Compilation flags
LATEX_FLAGS = -interaction=nonstopmode -shell-escape -output-directory=$(BUILD_DIR)

# PythonTeX settings
PYTHONTEX = pythontex
PYTHONTEX_FLAGS = --interpreter python:python3

# Default target
.PHONY: all clean cleanall help test-deps check-python

all: $(BUILD_DIR)/$(MAIN).pdf

# Main compilation target with PythonTeX
$(BUILD_DIR)/$(MAIN).pdf: $(MAIN).tex code/*.py | $(BUILD_DIR)
	@echo "=== Computational Physics Template Compilation ==="
	@echo "Building: $@"

	# First LaTeX pass - generates PythonTeX files
	@echo "1. Initial LaTeX compilation..."
	$(LATEX) $(LATEX_FLAGS) $(MAIN).tex

	# Check if PythonTeX files were generated
	@if [ -f "$(BUILD_DIR)/$(MAIN).pytxcode" ]; then \
		echo "2. Running PythonTeX (quantum simulations)..."; \
		cd $(BUILD_DIR) && $(PYTHONTEX) $(PYTHONTEX_FLAGS) $(MAIN).pytxcode; \
		echo "3. Final LaTeX compilation..."; \
		cd .. && $(LATEX) $(LATEX_FLAGS) $(MAIN).tex; \
		$(LATEX) $(LATEX_FLAGS) $(MAIN).tex; \
	else \
		echo "2. No PythonTeX code found, completing compilation..."; \
		$(LATEX) $(LATEX_FLAGS) $(MAIN).tex; \
	fi

	@echo "=== Compilation Complete ==="
	@echo "Output: $(BUILD_DIR)/$(MAIN).pdf"
	@ls -la $(BUILD_DIR)/$(MAIN).pdf

# Create build directory
$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)
	@mkdir -p $(BUILD_DIR)/assets
	@echo "Created build directory: $(BUILD_DIR)"

# Quick compilation (no PythonTeX - for drafts)
draft: $(MAIN).tex | $(BUILD_DIR)
	@echo "=== Draft Mode (No Computations) ==="
	$(LATEX) $(LATEX_FLAGS) $(MAIN).tex
	$(LATEX) $(LATEX_FLAGS) $(MAIN).tex

# Force full recompilation
rebuild: cleanall all

# Test Python dependencies
test-deps:
	@echo "=== Testing Python Dependencies ==="
	@python3 -c "import numpy; print(f'NumPy: {numpy.__version__}')"
	@python3 -c "import scipy; print(f'SciPy: {scipy.__version__}')"
	@python3 -c "import matplotlib; print(f'Matplotlib: {matplotlib.__version__}')"
	@python3 -c "import sys; exec('try:\\n import numba\\n print(\"Numba:\", numba.__version__)\\nexcept ImportError:\\n print(\"Numba: Not available (optional)\")')"
	@echo "All required dependencies available!"

# Check Python syntax in code files
check-python:
	@echo "=== Checking Python Code Syntax ==="
	@for file in code/*.py; do \
		echo "Checking $$file..."; \
		python3 -m py_compile $$file; \
	done
	@echo "All Python files are syntactically correct!"

# Run standalone Python simulations (for testing)
test-simulations:
	@echo "=== Testing Computational Simulations ==="
	@echo "Testing quantum mechanics..."
	@cd code && python3 quantum_mechanics.py
	@echo "Testing statistical mechanics..."
	@cd code && python3 statistical_mechanics.py
	@echo "Testing condensed matter..."
	@cd code && python3 condensed_matter.py
	@echo "All simulations completed successfully!"

# Continuous compilation (watches for changes)
watch: | $(BUILD_DIR)
	@echo "=== Watching for changes (Ctrl+C to stop) ==="
	@echo "Watching: $(MAIN).tex, code/*.py"
	@while true; do \
		if [ "$(MAIN).tex" -nt "$(BUILD_DIR)/$(MAIN).pdf" ] || \
		   [ "`find code -name '*.py' -newer $(BUILD_DIR)/$(MAIN).pdf 2>/dev/null`" ]; then \
			echo "Changes detected, recompiling..."; \
			$(MAKE) all; \
		fi; \
		sleep 2; \
	done

# Clean temporary files
clean:
	@echo "Cleaning temporary files..."
	@rm -f $(BUILD_DIR)/*.aux $(BUILD_DIR)/*.log $(BUILD_DIR)/*.toc
	@rm -f $(BUILD_DIR)/*.fls $(BUILD_DIR)/*.fdb_latexmk
	@rm -f $(BUILD_DIR)/*.pytxcode $(BUILD_DIR)/*.pytxmcr $(BUILD_DIR)/*.pytxpyg
	@rm -f $(BUILD_DIR)/pythontex-files-*/*
	@rmdir $(BUILD_DIR)/pythontex-files-* 2>/dev/null || true

# Clean everything including PDF output
cleanall: clean
	@echo "Cleaning all output files..."
	@rm -f $(BUILD_DIR)/$(MAIN).pdf
	@rm -rf $(BUILD_DIR)/assets/*
	@rmdir $(BUILD_DIR)/assets 2>/dev/null || true
	@rmdir $(BUILD_DIR) 2>/dev/null || true

# Install required packages (Ubuntu/Debian)
install-deps:
	@echo "=== Installing Dependencies ==="
	@echo "Installing LaTeX packages..."
	sudo apt-get update
	sudo apt-get install texlive-latex-extra texlive-science texlive-fonts-recommended
	@echo "Installing Python packages..."
	pip3 install numpy scipy matplotlib numba pythontex
	@echo "Dependencies installed!"

# Performance benchmark
benchmark: | $(BUILD_DIR)
	@echo "=== Performance Benchmark ==="
	@echo "Timing full compilation..."
	@time $(MAKE) rebuild
	@echo "PDF size: `du -h $(BUILD_DIR)/$(MAIN).pdf | cut -f1`"
	@echo "Assets generated: `ls $(BUILD_DIR)/assets/ 2>/dev/null | wc -l`"

# Validation checks
validate: test-deps check-python
	@echo "=== Template Validation ==="
	@echo "✓ Dependencies available"
	@echo "✓ Python syntax correct"
	@echo "Running test compilation..."
	@$(MAKE) cleanall
	@$(MAKE) all
	@if [ -f "$(BUILD_DIR)/$(MAIN).pdf" ]; then \
		echo "✓ PDF generated successfully"; \
		echo "✓ Template validation passed!"; \
	else \
		echo "✗ PDF generation failed"; \
		exit 1; \
	fi

# Help target
help:
	@echo "Computational Physics LaTeX Template - Build System"
	@echo "=================================================="
	@echo ""
	@echo "Primary targets:"
	@echo "  all         - Full compilation with PythonTeX simulations"
	@echo "  draft       - Quick compilation without computations"
	@echo "  rebuild     - Clean and rebuild everything"
	@echo ""
	@echo "Development targets:"
	@echo "  watch       - Continuous compilation on file changes"
	@echo "  validate    - Run full template validation"
	@echo "  benchmark   - Performance timing test"
	@echo ""
	@echo "Testing targets:"
	@echo "  test-deps   - Check Python dependencies"
	@echo "  check-python- Validate Python syntax"
	@echo "  test-simulations - Run standalone simulations"
	@echo ""
	@echo "Maintenance targets:"
	@echo "  clean       - Remove temporary files"
	@echo "  cleanall    - Remove all generated files"
	@echo "  install-deps- Install required packages"
	@echo ""
	@echo "Requirements:"
	@echo "  - pdflatex with -shell-escape support"
	@echo "  - Python 3 with NumPy, SciPy, Matplotlib"
	@echo "  - PythonTeX package"
	@echo ""
	@echo "For SEO-optimized computational physics templates:"
	@echo "  https://cocalc.com/scientific-templates"

# Debug information
debug:
	@echo "=== Debug Information ==="
	@echo "LaTeX compiler: `which $(LATEX)`"
	@echo "Python version: `python3 --version`"
	@echo "PythonTeX: `which $(PYTHONTEX) 2>/dev/null || echo 'Not found'`"
	@echo "Build directory: $(BUILD_DIR)/"
	@echo "Main file: $(MAIN).tex"
	@echo "Working directory: `pwd`"
	@echo "Available targets: all draft rebuild test-deps check-python"
	@echo "                  test-simulations watch clean cleanall"