#################################################### # file: Makefile # Makefile for SfS Problem Sheets # Manuel Koller, 10.02.2011 # # This borrows a lot from latexmake # http://xpt.sourceforge.net/ # (Date: 2006/10/12 18:13:16 Revision: 1.18) # # as well from the Makefile of # Luc Villandré, villandl@dms.umontreal.ca # https://stat.ethz.ch/pipermail/r-help/2009-December/222327.html # # Readme begins: # # This makefile can be used to generate .pdf and .R # from latex and Sweave files. To configure use config.mk. # # To get help, type 'make help' # # Readme ends. # {{{ Commentary: # This is a Makefile for the SfS Problem Sheets. # # Either just issue # make # which will sweave all the *.[Rr]nw files in the current directory # and all subdirectories (to change, see config.mk) and then # run pdflatex on all .tex files in the current directory. # Then all Sweave files are tangled. Finally all output files are removed. # You can issue # make check # to display the configuration and a list of dependent files. # To specify the master file explicitly, # for example 'master.Rnw': # - for the current and future sessions: # set it explicitly in config.mk # - for the current session only, issue # export TEX_MASTER="master" # make # Options: # Verbose-Output: by default make will swallow all output of # Sweave, texi2pdf, etc. This can be changed using the # verbose variable. Use # make (command) verbose=1 # all the output will then be shown. (See also make view-log.) # Other commands: # # make all : (default) like make pdf; make clean # make pdf : run pdflatex for all master files # make R : extract R code from all sweave files # make tangle : synonym to 'make R' # make Rout : Run R code and save results in .Rout # make bib : run bibtex # make tex : run just sweave (but not latex) # make edit : open all source files in emacs # make view : view output in evince # make view-log : view log files # make clean : delete all temporary output files # make clean-bak : delete all emacs backup files (e.g., *.tex~) # make clean-R : delete all .R output files # make clean-all : delete all of the above # make dist-clean : delete all of the above and the output .pdf # make check : display master and list of dependent files # make config : create config.mk file (optional) # make add-deps : add dependencies to config.mk (optional) # make list : display a list of available commands # make help : display help # }}} end commentary. # STOP STOP STOP STOP STOP STOP STOP STOP STOP # DO NOT EDIT ANYTHING BELOW BUT # USE make config AND THEN EDIT config.mk. # STOP STOP STOP STOP STOP STOP STOP STOP STOP # {{{ Load config file: -include config.mk # }}} # {{{ Defines: # Files # find all Rnw, tex and bib files in the current directory RNWFILES = $(wildcard *.[Rr]nw) TEXFILES = $(wildcard *.tex) BIBFILES = $(wildcard *.bib) # add info.sty to dependencies, if DEPENDS is undef ifndef DEPENDS INFOFILES = $(wildcard ../info.sty) $(wildcard info.sty) endif # If TEX_MASTER is not set, build all .Rnw and .tex files ifndef TEX_MASTER TEX_MASTER := $(sort $(basename $(RNWFILES) $(TEXFILES))) endif # inclusion of .Rnw, .tex, .bib in subdirectories # NO_SUBDIRS set in config.mk ifndef NO_SUBDIRS RNWFILES += $(wildcard */*.[Rr]nw) TEXFILES += $(wildcard */*.tex) BIBFILES += $(wildcard */*.bib) endif ## ignore files in IGNORE ifdef IGNORE RNWFILES := $(filter-out $(IGNORE), $(RNWFILES)) TEXFILES := $(filter-out $(IGNORE), $(TEXFILES)) BIBFILES := $(filter-out $(IGNORE), $(BIBFILES)) endif # Build output files and dependency list TEX_MASTER_FULL = $(foreach master, $(TEX_MASTER), $(firstword $(wildcard $(master).[Rr]nw) $(wildcard $(master).tex))) RNWFILES += $(filter %.Rnw %.rnw, $(TEX_MASTER_FULL)) $(filter %.Rnw %.rnw, $(DEPENDS)) TEXFILES += $(filter %.tex, $(TEX_MASTER_FULL)) $(filter %.tex, $(DEPENDS)) GENTEXFILES = $(patsubst %.Rnw,%.tex, $(subst .rnw,.Rnw,$(RNWFILES))) TEXFILES := $(filter-out $(GENTEXFILES), $(TEXFILES)) RFILES = $(patsubst %.Rnw,%.R,$(subst .rnw,.Rnw,$(RNWFILES))) ROUTFILES = $(patsubst %.R,%.Rout,$(RFILES)) DEPENDS += $(TEXFILES) $(BIBFILES) $(INFOFILES) SOURCE_FILES = $(filter-out $(GENTEXFILES) $(TEX_MASTER_FULL), $(RNWFILES) $(TEXFILES)) # .............................................................. &ss ... # here's the system dependent stuff # ifndef TEXEDITOR TEXEDITOR = emacs endif ifndef PDFLATEX PDFLATEX = pdflatex ifndef verbose PDFLATEX = texi2pdf -q -p else PDFLATEX = texi2pdf -b -p endif endif ifndef PDFVIEWER PDFVIEWER=evince endif ifndef R R = R endif ifndef SWEAVE ifndef ENCODING SWEAVE = $(R) CMD Sweave --encoding="UTF-8" else SWEAVE = $(R) CMD Sweave --encoding="$(ENCODING)" endif endif ifndef PGFSWEAVE PGFSWEAVE = $(R) CMD pgfsweave endif ifndef STANGLE ifndef ENCODING STANGLE = $(R) CMD Stangle --encoding="UTF-8" else STANGLE = $(R) CMD Stangle --encoding="$(ENCODING)" endif endif ifndef RBATCH RBATCH = $(R) CMD BATCH --no-save endif ifndef MAKEFILE MAKEFILE=Makefile endif # }}} # {{{ Canned command sequences ifndef verbose define run-sweave if grep -q --extended-regexp "({|<<).*(tikz|pgf)=(TRUE|FALSE)" "$(notdir $<)"; then \ $(PGFSWEAVE) "$(notdir $<)" 2>&1 > "$(basename $(notdir $<)).Rlog"; \ else \ $(SWEAVE) "$(notdir $<)" 2>&1 > "$(basename $(notdir $<)).Rlog"; fi endef else define run-sweave if grep -q --extended-regexp "({|<<).*(tikz|pgf)=(TRUE|FALSE)" "$(notdir $<)"; then \ $(PGFSWEAVE) "$(notdir $<)"; \ else \ $(SWEAVE) "$(notdir $<)"; fi endef endif define run-rnw2tex @echo "Sweaving $<" @cd "$(dir $<)"; \ $(run-sweave) endef define create-backup if [ -f "$(notdir $@).last" ]; then \ mv "$(notdir $@).last" "$(notdir $@).lastT"; fi; \ if [ -f "$(notdir $@)" ]; then \ echo "Creating backup of $(notdir $@) to $(notdir $@).last"; \ mv "$(notdir $@)" "$(notdir $@).last"; fi; endef define run-rnw2R @echo "Tangling $<" @cd "$(dir $<)"; \ $(create-backup) \ $(STANGLE) "$(notdir $<)" @if [[ ! -s "$@" ]]; then echo "File is empty, removing it."; rm "$@"; fi endef ifneq "$(GENTEXFILES)" "" define run-maketex @$(MAKE) -s tex endef endif define run-tex2pdf @echo "Rendering $@" @cd "$(dir $@)"; \ $(PDFLATEX) "$(basename $(notdir $@)).tex" endef define run-R2Rout @echo "Running $<" @cd "$(dir $<)"; \ if [ -f "$(notdir $<)" ]; then \ $(create-backup) \ $(RBATCH) "$(notdir $<)"; \ fi endef # }}} # {{{ Rules: %.tex: %.rnw $(run-rnw2tex) %.tex: %.Rnw $(run-rnw2tex) %.R: %.rnw $(run-rnw2R) %.R: %.Rnw $(run-rnw2R) %.pdf: %.tex $(run-tex2pdf) %.Rout: %.R $(run-R2Rout) # }}} # {{{ Targets: all: pdf clean pdf: $(addsuffix .pdf, $(TEX_MASTER)) $(addsuffix .pdf, $(TEX_MASTER)): $(DEPENDS) $(SOURCE_FILES) $(TEX_MASTER_FULL) $(run-maketex) $(run-tex2pdf) tangle: $(RFILES) R: tangle Rout: $(ROUTFILES) bib: $(BIBFILES) $(foreach master, ${TEX_MASTER}, bibtex $(master);) tex: $(GENTEXFILES) view: pdf $(PDFVIEWER) $(addsuffix .pdf, $(TEX_MASTER)) & edit: $(TEXEDITOR) ${TEX_MASTER_FULL} ${SOURCE_FILES} & .PHONY: check view-log clean clean-bak clean-R clean-all list help dist-clean config view-log: @if [ ! -f ${firstword ${addsuffix .log, $(TEX_MASTER)}} ]; then \ echo "Log files missing, use 'make pdf' instead of just 'make'"; else \ $(PAGER) ${addsuffix .log, $(TEX_MASTER)}; fi check: @if [ ! -f config.mk ]; then \ echo "Warning: config.mk is missing. please run 'make config'"; fi @echo "Master file(s): $(TEX_MASTER_FULL) ($(TEX_MASTER))" @[ $(NO_SUBDIRS) ] && echo "Looking for files in the current directory: NO_SUBDIRS set." \ || echo "Looking for files in subdirectories: NO_SUBDIRS not set." @echo "Source files: $(SOURCE_FILES)" @echo "Temporary output files: $(GENTEXFILES)" @echo "Dependencies: $(DEPENDS)" clean: @rm -vf $(foreach suffix, .dvi .log .aux .bbl .blg .ilg .toc .lof .lot .idx .ind .out .fff .ttt .map .sh .Rlog, $(addsuffix $(suffix), $(TEX_MASTER))) \ $(GENTEXFILES) $(patsubst %.tex,%.Rlog,$(GENTEXFILES)) @rm -rvf auto */auto ../auto/info.el @if [ -d ../auto ]; then rmdir --ignore-fail-on-non-empty ../auto; fi @rm -vf ex*/ex*.pdf ex*/Rplots.pdf clean-bak: @rm -vf *~ */*~ \ $(addsuffix .last, $(RFILES) $(ROUTFILES)) \ $(addsuffix .lastT, $(RFILES) $(ROUTFILES)) clean-R: @rm -vf $(RFILES) $(ROUTFILES) clean-all: clean clean-bak clean-R dist-clean: clean-all @rm -vf $(addsuffix .pdf, $(TEX_MASTER)) @rm -vf $(ROUTFILES) # {{{ Dummy config file begins: # ################################################# # # configuration file for Sweave and tex Makefile # # Manuel Koller, 10.02.11 # # # # 1) set master file: all files for which a .pdf should be produced. # # if, for example, 'master.tex' is the master file # # uncomment the next three lines to set master file explicitly: # # ifndef TEX_MASTER # # TEX_MASTER = master # # endif # # 2) automatic inclusion of files in subdirectories # # should all files in subdirectories be included when sweaving? # # if no, uncomment the following line else comment it # # NO_SUBDIRS = T # # 3) custom dependencies # # To track dependencies not automatically found by this # # Makefile, add them below (multiple files separated by space) # # run make add-deps to try to add dependencies automatically # DEPENDS = # # 4) files to be ignored, e.g., not to be sweaved # # separate multiple files by space # # IGNORE = # # 5) force encoding for Sweave (default is UTF-8) # # to avoid warnings like # # "Warning: ‘ex.Rnw’ has unknown encoding: assuming Latin-1" # # or to set the encoding to something else than UTF-8, # # uncomment the following line and set the desired encoding. # # ENCODING = UTF-8 # }}} end dummy config file. # Self maintenance config: @if [ ! -f config.mk ]; then \ cat ${MAKEFILE} | sed -n '1,/ Dummy config/d; / end dummy config/q; p' | cut -c3- > config.mk; \ emacs config.mk; \ else \ echo "There is already a file config.mk."; \ fi FOUNDINPUTS = $(shell perl ~kollerma/Tools/findinputs-recursive.pl $(TEX_MASTER_FULL)) add-deps: @if [ ! -f config.mk ]; then \ cat ${MAKEFILE} | sed -n '1,/ Dummy config/d; / end dummy config/q; p' | cut -c3- > config.mk; \ fi @echo "DEPENDS = $(sort $(SOURCE_FILES) $(FOUNDINPUTS))" >> config.mk @echo "Added dependencies to config.mk" list: @grep -E '^[a-zA-Z0-9_\-]+[ ]*:' ${MAKEFILE} | \ awk -F':' '{print $$1 }' help: @cat ${MAKEFILE} | sed -n '1,/ Commentary/d; / end commentary/q; p' | cut -c3- | more # delete files on error .DELETE_ON_ERROR: # }}}