pgfSweave Makefile 0.2

The weekend was spent cleaning and home improving for Sienna. But it didn’t happen before I’d communicated with one of the main pgfSweave developers and realized that the real goal is to avoid repeating R code in the woven document. That is, all R code should be written only once. This is a more reliable programming approach, since every change happens only in one place. And it turns out that my original contribution was not much of a contribution, as \SweaveInput does effectively the same thing.

So this combo compelled me to see if I could keep the R code unique and process it over several input files. I finally figured out how to do it Friday night. The result is a minor modification of the previous version. Unfortunately, it needs more direct manipulation for each particular compilation. And while it does not appear to have the speed of pgfSweave, some unreliable tests indicate that this file may be more reliable in incorporating textual changes. And you still get LaTex typesetting.

(And now that it’s functional for my desires,  I’ve purged this from my system and moved on to awesome.)

# Makefile for pgfSweave and complex latex projects — allows LaTeX typesetting and sequential R code across included files, but may not take advantage of pgfSweave’s caching abilities
#
# Author: cuz
# Doctoral candidate
# Dept. of Urban Planning
# Graduate School of Architecture, Planning and Preservation
# Columbia University
#
# 02 June 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.
#
# Note: The previous paragraph, I have been informed, is not quite right.
#
# It is also not clear at this time if this approach actually takes advantage of the caching offered by pgfSweave. It does, however, capture the LaTeX typesetting.
#
# 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.
#
# To be more confident that your variables are reset when you start making your file and when you finish, I recommend including the following line at the beginning and end of your master document.
# <<resetvars,echo=T>>=
# rm(list=ls(all=TRUE))
# @
# This file makes use of the –save and –restore options for R, which is not advised if you don’t know what you’re doing. And, uh, I don’t know what I’m doing.#
# Also, you must list the order of included file dependencies in the INCLUDED FILES SEQUENCE section near the end.
#
# As of this date, I do not think you can use \includeonly on this, but probably.
#
TARGET = testmaster
MASTER = $(TARGET).pdf
BONDSMAN = $(TARGET).tex

# We aren’t finished until master.pdf is made.
all: $(MASTER)

# $(TARGET).Rnw:

# 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 = $(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 –restore –save

# INCLUDED FILES SEQUENCE
#
# This is the inconvenient part of the file. Unless your files have a consistent ordinally or alphabetically defined sequence, e.g., file1.xxx, file2.xxx, file3.xxx, etc, where file3’s R code depends on file2’s code and file2 in turn on file1, you must specifically list the order here.
#
# E.g.,
# file2.tex: file1.tex
#
# file3.tex: file2.tex

clean:
# 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))

  1. No comments yet.

  1. No trackbacks yet.