# Runs pdflatex twice if it works, otherwise deletes the pdf output which isn't valid.
# It deletes the pdf because that way you can fix the error and re-run make without
# deleting the broken pdf yourself.
%.pdf : %.tex
	@find . -name $*.aux -cnewer $*.tex -exec rm {} \;
	@if [ ! -f $*.aux ]; then \
		echo $(PDFLATEX) $*.tex; \
		env TEXINPUTS=.:LaTeX:: $(PDFLATEX) $*.tex; \
	fi && env TEXINPUTS=.:LaTeX:: $(PDFLATEX) $*.tex || $(RM) $*.pdf
#
PDFLATEX = pdflatex
#
clean :
	$(RM) *.log *.dvi *.aux *.toc *.log *.out *~ core

empty : clean
	@for f in `find . -maxdepth 1 -name \*.pdf`; do \
	    if [ -f `echo $$f | sed 's/pdf/tex/'` ]; then \
		echo $(RM) $$f; \
		$(RM) $$f; \
	    fi; \
	done
