pgfSweave Makefile
I just realized that the file below probably constitutes my first contribution to open source software, even if my contributions are really the minor ones. In order to elegantly present my dissertation, I’ve been avoiding writing it in favor of getting pgfSweave to work with my project. (See next post.) A good chunk of the last several days was spent trying to write a makefile that would build my final pdf for me. To save others the pain, I’ve decided to share my file and comment it copiously.
If you’re using LaTeX, R, and pgfSweave in a project that involves using “\include{}” to separate out your chapters or other chunks of writing, this code’s for you.
# Makefile for pgfSweave and complex latex projects
#
# Author: cuz
# Doctoral candidate
# Dept. of Urban Planning
# Graduate School of Architecture, Planning and Preservation
# Columbia University
#
# 28 May 2009
#
# This Makefile comes substantially from work by Nicholas Lewin-Koh, Rouben Rostmaian, Deepayan Sarkar, Johannes Ranke, and Mark Wardle. It involves minor changes to allow the use of the wonderful pgfSweave package. And, of course, any improvements or corrections are welcome.
#
# I do not have any deep understanding of what is going on in this, but it seems to be working (on Ubuntu 9.04). Use at your own risk.
#
# There is all the information you need here: http://www.gnu.org/software/make/manual/ .
# I also found this introduction helpful: http://www.jfranken.de/homepages/johannes/vortraege/make_inhalt.en.html#ToC10 .
#
# Because pgfSweave defaults to producing a .pdf, it cannot be used directly in documents that include “\include”s. I believe this is because it only processes the main document (?).
# Instead, pgfSweave must be told not to compile the .tex files it produces. Rather, they must be processed first through texi2dvi to make sure all cross-references (including bibliographies) are resolved. Then the .tex files can be turned into .pdfs with pdflatex.
#
# FOR THE COMPLETELY CLUELESS, as I was a couple of days ago, to use a makefile, simply copy this file into the folder that contains your .Rnw files. Make sure it has a name like “Makefile” (recommended) or “makefile”. When you’re ready to roll, navigate to the directory with all your files and type “make”. Et voilà! Pretty amazing stuff.
#
# Change “master” to the base nume of your master file (without the file type suffix) in the next line. This will be from a .Rnw,.rnw,.Snw,.snw file (or else you wouldn’t be using this Makefile!). The file will then provide the name for the .tex file that will ultimately be needed and the .pdf file that will be produced.
#
TARGET = master
MASTER = $(TARGET).pdf
BONDSMAN = $(TARGET).tex# We aren’t finished until master.pdf is made.
all: $(MASTER)# Makes a list of all .Rnw files in the folder. You’ll have to change the suffix if you’re using something else.
RNWFILES = $(wildcard *.Rnw)
# Makes list of all existing .tex files. This way, if there are .tex files without code in them, they will be included in the list of DEPENDS files (I think). This will in turn ensure that citations and cross-references are included in the final document.
TEXFILES = $(wildcard *.tex)
# This compiles a list of all the .tex files that will be needed by adding all existing .tex files to a list of .Rnw files with the filetype suffix changed to .tex (through patsubst). Though functionally unnecessary in my experience so far, for cleanliness, I have had master.tex filtered out from the list, since it is an almost final product rather than a proper dependency.
DEPENDS = $(filter-out $TARGET,$(patsubst %.Rnw,%.tex,$(RNWFILES))) $(TEXFILES)# Process the .tex files through texi2dvi to work out all cross-references and then process them into a .pdf.
$(MASTER): $(BONDSMAN)
@texi2dvi ‘$<’
@pdflatex ‘$<’# The master .tex file depends on having all the other .tex files up to date.
$(BONDSMAN): $(DEPENDS)# Turns .Rnw files into .tex files by processing them through pgfSweave. It does so by piping a command to R. I believe –no-restore and –no-save ensure that the R session is a clean one and opens and exits without need for input.
%.tex: %.Rnw
(echo “library(pgfSweave); pgfSweave(‘$<’,compile.tex = FALSE)”) | R –no-restore –no-saveclean:
# Typing “make clean” in the directory where all this is processed will remove all of the following file types and files.
@rm -f *.aux *.bbl *.blg *.brf *.cb *.dvi *.eps *.fff *.ind *.idx *.ilg *.inx *.lot *.lof *.log *.map *.ps *.out *.sh *.toc *.ttt RPlots.*
# It will also remove all of the .tex files that were generated from .Rnw files.
@rm -f $(patsubst %.Rnw,%.tex,$(RNWFILES))
Greatest thing ever.