Created
April 8, 2013 22:57
-
-
Save lahwaacz/5341286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makefile for LaTeX files | |
# Original Makefile from http://www.math.psu.edu/elkin/math/497a/Makefile | |
# Please check http://www.acoustics.hut.fi/u/mairas/UltimateLatexMakefile | |
# for new versions. | |
# Copyright (c) 2005,2012 (in order of appearance): | |
# Matti Airas <Matti.Airas@hut.fi> | |
# Rainer Jung | |
# Antoine Chambert-Loir | |
# Timo Kiravuo | |
# Jakub Klinkovský <kuba.klinkovsky@gmail.com> | |
# Changelog (since 2012): | |
# 12.10.2012 simple gnuplot generation, filename specification in \input | |
# and \include commands in LaTeX can be with or without the | |
# .tex extension | |
# 14.10.2012 partialy fixed the "Rerun to get cross-references right." | |
# issue - always runs three times, but it's not needed | |
# 14.10.2012 Added 'grayscale' option. Use grayscale=1 or grayscale=0, | |
# the default is 0. | |
# TODO: latex always writes "Rerun to get cross-references right." but it's OK. | |
LATEX = latex -synctex=1 -interaction=batchmode --src-specials | |
PDFLATEX = pdflatex -synctex=1 -interaction=batchmode --src-specials | |
BIBTEX = bibtex | |
MAKEINDEX = makeindex | |
XDVI = xdvi -gamma 4 | |
DVIPS = dvips | |
DVIPDF = dvipdft | |
#MAKEDJVU= djvudigital --words | |
MAKEDJVU= pdf2djvu -q | |
DJVUVIEW= djview | |
L2H = mk4ht xhmlatex | |
GH = qpdfview | |
PDFVIEW = qpdfview | |
CONVERT = convert | |
GZIP = gzip --best | |
GNUPLOT = gnuplot | |
#Colors | |
ifndef nocolor | |
gray=\e[1;30m | |
red=\e[0;31m | |
RED=\e[1;31m | |
blue=\e[0;34m | |
BLUE=\e[1;34m | |
green=\e[0;32m | |
GREEN=\e[1;32m | |
cyan=\e[0;36m | |
CYAN=\e[1;36m | |
NC=\e[0m # No Color | |
endif | |
RERUN = "(Rerun to get (cross-references|the bars) right)" | |
RERUNBIB= "No file.*\.bbl|Citation.*undefined" | |
MAKEIDX = "^[^%]*\\makeindex" | |
MPRINT = "^[^%]*print" | |
USETHUMBS = "^[^%]*thumbpdf" | |
TMPDIR = generated | |
LTITLE = LaTeX | |
DATE=$(shell date +%Y-%m-%d) | |
COPY = if test -r $(<:%.tex=%.toc); then cp $(<:%.tex=%.toc) $(<:%.tex=%.toc.bak); fi > /dev/null | |
RM = rm -f | |
QUIET = &>/dev/null | |
DISPLAYERROR = || ( echo -e "$(BLUE)==> $(RED)Errors encountered:$(NC)" && perl -0 -ne '$$now=0;$$previous=0;@matches=/(![^.]*[.]\s*\n(?:[^l].*\n)*(l[.][0-9]+.*)\n)/g;foreach $$text (@matches) { if ($$text =~ /^l[.][0-9]+.*$$/) { $$previous = $$text } else { $$text =~ /\n(l[.][0-9]+.*)/; $$now=$$1; if ( $$previous ne $$now ) { print "\n$$text\n" } } }' $(<:%.tex=%.log) && false ) | |
SRC := $(shell egrep -l '^[^%]*\\begin\{document\}' *.tex) | |
TRG := $(SRC:%.tex=%.dvi) | |
PSF := $(SRC:%.tex=%.ps) | |
PSZ := $(SRC:%.tex=%.ps.gz) | |
PDF := $(SRC:%.tex=%.pdf) | |
DJVU := $(SRC:%.tex=%.djvu) | |
# GRAYSCALE option forces grayscale output (useful for printing) | |
ifeq ($(grayscale), 1) | |
LATEX += "\"\def\grayscaleoutput{}\input{$(SRC)}\"" | |
PDFLATEX += "\"\def\grayscaleoutput{}\input{$(SRC)}\"" | |
# GNUPLOT_TERMINAL += "color colortext" | |
#else | |
# GNUPLOT_TERMINAL += "monochrome" | |
endif | |
define run-latex | |
$(COPY) $(QUIET);\ | |
echo -e "$(BLUE)==> $(green)Running $(NC)$(LTITLE)$(green) | $(red)First Run$(NC)";\ | |
$(LATEX) $< $(QUIET) $(DISPLAYERROR) ; runs=1; \ | |
if egrep $(MAKEIDX) $< $(QUIET);\ | |
then\ | |
echo -e "$(BLUE)==> $(green)Generating index$(NC)";\ | |
$(MAKEINDEX) $(<:%.tex=%) $(QUIET);\ | |
$(COPY) $(QUIET);\ | |
echo -e "$(BLUE)==> $(green)Running $(NC)$(LTITLE)$(green) | $(red)Including Index$(NC)";\ | |
$(LATEX) $< $(QUIET) $(DISPLAYERROR); let "runs=runs+1"; \ | |
fi;\ | |
bibrun=1;\ | |
while egrep -c $(RERUNBIB) $(<:%.tex=%.log) $(QUIET);\ | |
do\ | |
if [ "$$bibrun" -eq "1" ];\ | |
then\ | |
echo -e "$(BLUE)==> $(green)Parsing Bibliography$(NC)";\ | |
$(getbibs);\ | |
for file in $$bibs; do\ | |
mv $$file $$file.bak;\ | |
python2 ./unicode_to_latex.py $$file.bak $$file;\ | |
done;\ | |
$(BIBTEX) $(<:%.tex=%) $(QUIET);\ | |
for file in $$bibs; do\ | |
mv $$file.bak $$file;\ | |
done;\ | |
fi;\ | |
echo -e "$(BLUE)==> $(green)Running $(NC)$(LTITLE)$(green) | $(red)Applying bibliography (run $$bibrun)$(NC)";\ | |
$(COPY) $(QUIET);\ | |
$(LATEX) $< $(QUIET) $(DISPLAYERROR);\ | |
let "bibrun=bibrun+1";\ | |
done;\ | |
try=1;\ | |
while egrep -c $(RERUN) $(<:%.tex=%.log) $(QUIET);\ | |
do\ | |
echo -e "$(BLUE)==> $(green)Running $(NC)$(LTITLE)$(green) | $(red)Solving References (run $$try)$(NC)";\ | |
$(COPY) $(QUIET);\ | |
$(LATEX) $< $(QUIET) $(DISPLAYERROR);\ | |
let "try=try+1"; \ | |
if [ $$try -gt 3 ]; then break; fi;\ | |
done;\ | |
let "runs=runs + bibrun + try - 2";\ | |
if cmp -s $(<:%.tex=%.toc) $(<:%.tex=%.toc.bak) $(QUIET);\ | |
then\ | |
true;\ | |
else \ | |
if test -r $(<:%.tex=%.toc.bak); \ | |
then \ | |
echo -e "$(BLUE)==> $(green)Running $(NC)$(LTITLE)$(green) | $(red)Solving References (run $$try)$(NC)";\ | |
$(LATEX) $< $(QUIET) $(DISPLAYERROR);\ | |
let "runs=runs+1";\ | |
fi;\ | |
fi;\ | |
$(RM) $(<:%.tex=%.toc.bak) $(QUIET);\ | |
echo -e "$(BLUE)==> $(blue)Finished $(NC)$(LTITLE)$(blue) in $(red)$$runs$(blue) runs$(NC)"; | |
echo -e "$(BLUE)==> ${blue}Undefined references and citations:" | |
egrep -i "(Reference|Citation).*undefined" $(<:%.tex=%.log) && echo -n -e "$(NC)" || echo -e "$(green)None.$(NC)"; | |
echo -e "$(BLUE)==> $(blue)Dimensions:$(NC)"; | |
grep "dimension:" $*.log 2> /dev/null || echo -e "$(green)None.$(NC)"; | |
true; | |
endef | |
define get_dependencies | |
deps=`perl -ne '($$_)=/^[^%]*\\\(?:include|input)\{(.*?)\}/;@_=split /,/;foreach $$t (@_) {if ($$t =~ /.tex$$/) {print "$$t "} else {print "$$t.tex "}}' $<` | |
endef | |
define get_dependent_dirs | |
depdirs=`echo -n ". "; for dir in $$(for file in $$(perl -ne '($$_)=/^[^%]*\\\(?:include|input)\{(.*?)\}/;@_=split /,/;foreach $$t (@_) {print "$$t.tex "}' $<);do echo $$(dirname $$file); done | sort -u); do echo -n $$dir; echo -n " "; done` | |
endef | |
define getbibs | |
bibs=`perl -ne '($$_)=/^[^%]*\\\bibliography\{(.*?)\}/;@_=split /,/;foreach $$b (@_) {print "$$b.bib "}' $< $$deps` | |
endef | |
define getepss | |
epss=`perl -ne '@foo=/^[^%]*\\\(includegraphics|psfig|[a-zA-Z]+WallPaper\{[0-9]*\.?[0-9]*\})(\[.*?\])?\{(.*?)\}/g;if ($$foo[2] ne "") { if ($$foo[2] =~ /.eps$$/) { print "$(TMPDIR)/$$foo[2] "; } else { print "$(TMPDIR)/$$foo[2].eps "; }}' $< $$deps` | |
endef | |
define getpdfs | |
pdfs=`perl -ne '@foo=/^[^%]*\\\(includegraphics|[a-zA-Z]+WallPaper\{[0-9]*\.?[0-9]*\})(\[.*?\])?\{(.*?)\}/g;if ($$foo[2] ne "") { if ($$foo[2] =~ /.pdf$$/) { print "$(TMPDIR)/$$foo[2] "; } else { print "$(TMPDIR)/$$foo[2].pdf "; }}' $< $$deps` | |
endef | |
define getfigdirs | |
dirs="";\ | |
for file in $$epss $$pdfs;\ | |
do\ | |
dir=`dirname $$file`;\ | |
if (echo -e $$dirs | egrep -c $$dir) > /dev/null;\ | |
then\ | |
true;\ | |
else\ | |
dirs="$$dirs $$dir ";\ | |
fi\ | |
done | |
endef | |
define manconf | |
mandeps=`if test -r $(basename $@).cnf ; then cat $(basename $@).cnf |tr -d '\n\r' ; fi` | |
endef | |
# $(DEP) $(EPSPICS) $(BIBFILE) | |
$(TRG) : %.dvi : %.tex %.d | |
@$(run-latex) | |
$(PSF) : %.ps : %.dvi %.d | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(DVIPS) $< -o $@ $(QUIET) | |
$(PSZ) : %.ps.gz : %.ps | |
@echo -e "$(BLUE)==> $(green)Compressing $(gray)$@$(NC)" | |
@$(GZIP) -c $< > $@ | |
$(PDF) : LATEX = $(PDFLATEX) | |
$(PDF) : LTITLE = PDFLaTeX | |
$(PDF) : %.pdf : %.tex %.d | |
@$(run-latex) | |
#$(PDF) %.pdf : %.dvi | |
# @echo -e "$(BLUE)==> $(green)Generating Portable Document File$(NC)" | |
# @$(DVIPDF) -o $@ $< | |
$(DJVU) : %.djvu : %.pdf | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@($(MAKEDJVU) -o $@ $< ) $(QUIET) | |
# image conversion rules | |
$(TMPDIR)/%.eps : %.png | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) -compress zip $< eps3:$@ $(QUIET) | |
$(TMPDIR)/%.eps : %.tiff | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) -compress zip $< eps3:$@ $(QUIET) | |
$(TMPDIR)/%.eps : %.jpg | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) -compress zip $< eps3:$@ $(QUIET) | |
$(TMPDIR)/%.eps : %.bmp | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) -compress zip $< eps3:$@ $(QUIET) | |
$(TMPDIR)/%.eps : %.gif | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) -compress zip $< eps3:$@ $(QUIET) | |
$(TMPDIR)/%.eps : %.pdf | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@pdftops -eps -level3 $< $@ $(QUIET) | |
$(TMPDIR)/%.eps : %.ps | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@ps2eps -l -q $< | |
@mv $*.eps $@ | |
$(TMPDIR)/%.eps : %.eps | |
@echo -e "$(BLUE)==> $(green)Copying $(gray)$@$(NC)" | |
@cp $< $@ | |
$(TMPDIR)/%.eps : %.mp | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@mpost $< | |
@mv $*.1 $@ | |
$(TMPDIR)/%.eps : %.dia | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@dia --export=$@ --filter=eps $< $(QUIET) | |
$(TMPDIR)/%.pdf : %.pdf | |
@echo -e "$(BLUE)==> $(green)Copying $(gray)$@$(NC)" | |
@cp $< $@ | |
$(TMPDIR)/%.pdf : %.eps | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@epstopdf --outfile=$@ $< $(QUIET) | |
$(TMPDIR)/%.pdf : %.dia | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(green) | $(red)Step 1$(NC)" | |
@dia --export=$(<:.dia=.eps) --filter=eps $< $(QUIET) | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(green) | $(red)Step 2$(NC)" | |
@epstopdf --outfile=$@ $(<:.dia=.eps) $(QUIET) | |
@rm -f $(<:.dia=.eps) $(QUIET) | |
$(TMPDIR)/%.pdf : %.ps | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@ps2pdf14 -dPDFSETTINGS=/prepress $< $@ $(QUIET) | |
$(TMPDIR)/%.pdf : %.bmp | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) $< $@ | |
$(TMPDIR)/%.pdf : %.png | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) $< $@ | |
$(TMPDIR)/%.pdf : %.jpg | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) $< $@ | |
$(TMPDIR)/%.pdf : %.tif | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) $< $@ | |
$(TMPDIR)/%.pdf : %.tiff | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@$(CONVERT) $< $@ | |
$(TMPDIR)/%.pdf : %.mp | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@mptopdf $< $(QUIET) | |
@rm -f $*.1 $(QUIET) | |
@mv $*-1.pdf $@ | |
# Rules to run sage to generate graphs | |
$(TMPDIR)/%.eps : %.sage | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@sage $< $@ | |
@rm -f $(<:.sage=.py) $(QUIET) | |
$(TMPDIR)/%.pdf : %.sage | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@sage $< $@ | |
@rm -f $(<:.sage=.py) $(QUIET) | |
# Rules to run csv2latex to convert .csv to .tex | |
#%.tex : %.csv | |
# @echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
# @csv2latex --nohead --noescape --separator t --block none --position c $< > $@ | |
# generate graphs using gnuplot | |
# DO NOT USE "set terminal" and "set output" in your *.gpi files! | |
# TODO: generovat .tex do TMPDIR, v getpdfs() zjišťovat jestli *.tex existuje, jinak přesměrovat do TMPDIR | |
%.tex : %.gpi %.dat | |
@echo -e "$(BLUE)==> $(green)Generating $(gray)$@$(NC)" | |
@FILE=$(basename $@ .tex) && echo $$FILE && $(GNUPLOT) -e "filename = '$$FILE'" $< $(QUIET) | |
# This is a rule to generate a file of prerequisites for a given .tex file | |
%.d : %.tex | |
@echo -e "$(BLUE)==> $(green)Determining dependencies$(NC)" ;\ | |
$(get_dependencies) ; echo -e $$deps ;\ | |
$(getbibs) ; echo -e $$bibs ;\ | |
$(getepss) ;\ | |
$(getpdfs) ;\ | |
$(getfigdirs) ;\ | |
$(get_dependent_dirs) ;\ | |
$(manconf) ;\ | |
echo -e "$*.dvi $*.pdf $@ : $< $$deps $$bibs $$mandeps $(EXTRAS)" > $@ ; echo "" >> $@ ;\ | |
echo -e "$*.dvi : $$dirs $$epss" >> $@ ; echo "" >> $@ ;\ | |
echo -e "$*.pdf : $$dirs $$pdfs" >> $@ ; echo "" >> $@ ;\ | |
echo -e "DEPDIRS=$$depdirs" >> $@ ; echo "" >> $@ ;\ | |
if [[ $$dirs != "" ]]; then \ | |
echo -e "$$dirs :" >> $@ ;\ | |
echo -e " @echo \"$(BLUE)==> $(green)Creating $(gray)\$$@$(NC)\"" >> $@ ;\ | |
echo -e " @mkdir -p \$$@" >> $@ ;\ | |
fi | |
.PHONY: all show clean ps pdf html showps showpdf tidy clean-images pristine backup backupdir | |
all: $(TRG) | |
default: | |
echo "$(blue)done.$(NC)" | |
view: showpdf | |
show: showpdf | |
viewdjvu: showdjvu | |
showdjvu: djvu | |
@for i in $(DJVU); do \ | |
echo -e "$(BLUE)==> $(green)Viewing $(gray)$<: $$i$(NC)" ;\ | |
$(DJVUVIEW) $$i ;\ | |
done | |
viewdvi: showdvi | |
showdvi: $(TRG) | |
@for i in $(TRG); do \ | |
echo -e "$(BLUE)==> $(green)Viewing $(gray)$<: $$i$(NC)" ;\ | |
$(XDVI) $$i ;\ | |
done | |
viewps: showps | |
showps: ps | |
@for i in $(PSF); do \ | |
echo -e "$(BLUE)==> $(green)Viewing $(gray)$<: $$i$(NC)" ;\ | |
$(GH) $$i ;\ | |
done | |
viewpdf: showpdf | |
showpdf: pdf | |
@for i in $(PDF); do \ | |
echo -e "$(BLUE)==> $(green)Viewing $(gray)$<: $$i$(NC)" ;\ | |
$(PDFVIEW) $$i ;\ | |
done | |
psgz: $(PSZ) | |
postscript: ps | |
ps: $(PSF) | |
pdf: $(PDF) | |
djvu: pdf $(DJVU) | |
backupdir: | |
@ls backup $(QUIET) || echo -e "$(BLUE)==> $(green)Creating Backup Directory$(NC)" | |
@mkdir backup || true | |
backup: clean backupdir | |
@echo -e "$(BLUE)==> $(green)Backing up project$(NC)" | |
@export NUM=`ls backup/ | grep backup.tar.bz2 | wc -l` && tar jcvf ` echo -e "backup/backup.tar.bz2."$$NUM` `find ./ -not -type d -a -not -regex .*backup.* -a -not -regex .*svn.* | tail -n +2` | |
# TODO: This probably needs fixing | |
html: $(DEP) $(EPSPICS) | |
@$(L2H) $(SRC) $(QUIET) | |
refresh: | |
rm -f *.d | |
tidy: | |
@echo -e "$(BLUE)==> $(green)Cleaning up temporary files$(NC)" | |
@for dir in $(DEPDIRS); do \ | |
cd $$dir ;\ | |
rm -f *.aux *.bbl *.blg *.d *.idx *.ilg *.ind *.lof *.log *.lol *.lot *.out *.toc ;\ | |
cd - $(QUIET) ;\ | |
done | |
clean-images: | |
@echo -e "$(BLUE)==> $(green)Cleaning up generated images$(NC)" | |
@rm -rf $(TMPDIR) | |
clean: tidy clean-images | |
@echo -e "$(BLUE)==> $(green)Cleaning up output files$(NC)" | |
@rm -f $(TRG) $(PSF) $(PSZ) $(PDF) $(DJVU) $(SRC:%.tex=%.synctex.gz) | |
pristine: clean | |
@echo -e "$(BLUE)==> $(green)Removing backups$(NC)" | |
@rm -rf backup | |
-include $(SRC:.tex=.d) | |
# vim: noet sts=0 sw=8 ts=8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment