Makefile for dummies? Mac OS X

11.5k views Asked by At

I am having trouble understanding an answer to a previous question I asked here on stackoverflow located at:

More efficient R / Sweave / TeXShop work-flow?

The answer in particular is from user: las3rjock who suggests creating a "makefile" and running the makefile to automate compilation of a .Rnw in R (Sweave) and then in LaTeX. I don't know what a makefile is, or how to use it in Terminal (?) on Mac OS X. My internet searching has returned things above my knowledge level. I guess I need some hand holding to create and run a makefile.

Could anyone give "dummy" instructions on how to create a makefile to run Sweave / LaTeX, or more to the point, utilize the answer from las3rjock in the previous question? Thank you!

5

There are 5 answers

0
Jeromy Anglim On BEST ANSWER

I wrote a post with links on getting started with makefiles for Sweave.

From the post:

I also posted four sample Sweave documents. Each one has full source code available on github. Each one uses a makefile. In the first two tutorials, details of the makefile are described:

0
Spacedman On

A makefile is a plain text file that specifies how to make 'targets' given a set of 'dependencies' via a bunch of actions. The targets are the things on the left with ":" after them, the dependencies are on the rest of that line. Subsequent indented lines are the actions that should make the target.

So for example in your LaTeX case, the .pdf file is your final target, and that depends on your .tex file. So there's a an action that runs pdflatex. The .tex itself is created from the .Rnw file, so there's another rule to make foo.tex from foo.Rnw by running Sweave.

The beauty of make is that it checks the timestamps on dependencies. It was originally meant for compiling large C programs, divided up into lots of files. When compiling the final program there's no point recompiling all the .C that you've not changed, so your Makefile would just compile the changed .C files and link all the .o files into an executable.

Make has been leveraged way beyond that original scheme, to the point that newer automated build solutions are available, so you may see mentions of cmake, ant, maven, and a bunch of other things. I don't know what half of them are.

0
Matt Bannert On

I just read the previous question and this one, but here is a little shell script that I wrote for Mac OS X. I am not sure if that is exactly what you want, but maybe you can learn from this:

#!/bin/sh
R CMD Sweave yourfile.Rnw
for file in `ls pdfs`;
do pdfcrop "$file" pdfs/"$file"
done
pdflatex yourfile.tex
open yourfile.pdf

In case you do not know: you can make this script executable by chmod +x yourscriptfile.sh and then run it by ./yourscript.The first line starts your Sweave creation of .tex file. The for loop is optional: it's used to crop .pdf graphics to a subfolder. I found this much easier than handling the margin within R's plotting – in particular if I want to optimize the position in your .pdf report.

Workflow-wise I do the following: 1. store everything that is printed into R objects (such as function results and graphics). 2. Just use a little line in Sweave that sources the whole analysis. That way my Sweave document contains hardly any non LaTeX code and remains handy. Whenever I edit the text I just need to edit the sweave file (or if I do not need the calculations to be refreshed or even need only the .tex file)

HTH

0
Syl On

create a file named Makefile where your documents are stored and open it.

Copy/paste the code he typed in your Makefile (and remember to replace myfile by the name of the file you want to generate)

Open a console in your documents directory and type make pdf

0
stevec On

I arrived here looking for a simple example of a Makefile. So here's one:

# Makefile

showme:
        ls

important note that the gap on this line ls is actually 1 single press of the tab button. It cannot be spaces for weird reasons unique to make. If you do use spaces you'll get this error: "Makefile:4: *** missing separator. Stop.".

Once you have a file called Makefile with the above tiny snippet of code in it, in your terminal type make showme to run it. You should see the contents of the directory printed to the terminal.