# Sweave Engine for TeXShop

Sweave is an awesome utility for including the output of R code in a LaTeX document which I have started using regularly.

TeXShop is my favorite editor/viewer for LaTeX under Mac OS X for many reasons. One of which is the speed of the edit -> compile -> view process which it enables. Therefore, I have been reluctant to use Sweave in the past because of the extra LaTeX compilation step that is not supported by default in TeXShop.

Recently, my friend Charlie Sharpsteen first showed me this way to get around this with a custom “engine”. The engine file looke like this:

Sweave.engine

 #!/bin/bash export PATH=$PATH:/usr/texbin:/usr/local/bin R CMD Sweave "$1" pdflatex "${1%.*}"  Place this file in ~/Library/TeXShop/Engines/, make sure PATH includes the location of you TeX distribution. Make sure executable permissions (like 744) are set on both files or TeXShop will give an error message. Now open (or re-open) TeXShop and select “Sweave” from the list at the top left. Now you can use command-t to compile Sweave and view your document in one step! Thanks Chuck! # Using the booktabs package with Sweave and xtable The xtable package in R can output R data as latex tables. Used in conjunction with Sweave it is possible to automatically generate tables in a report. Needless to say this provides a really appealing possibility of never typing data into a table again. The only problem is that I think the default latex tables don’t look that great. Enter booktabs… The booktabs package in latex makes really nice tables, especially if there is math in your table that might run up against the regular \hline in the tabular environment. Buried away in the xtable documentation are some options that make this possible. See the example below:  \documentclass{article} \usepackage{booktabs} \begin{document} \begin{table}[!h] \centering \caption{Check out my table.} \label{tab:mytable} <<mytable,echo=F,results=tex>>= library(xtable) mat <- as.data.frame(matrix(rnorm(25),nrow=5)) colnames(mat) <- c("$\\alpha$","$\\beta$", "$\\gamma$","$\\delta$", "$\\frac{\\epsilon}{2}$") rownames(mat) <- c(‘A’,’B’,’C’,’D’,’E’) mat <- xtable(mat,digits=rep(5,ncol(mat)+1)) print(mat, sanitize.text.function = function(x){x}, floating=FALSE, hline.after=NULL, add.to.row=list(pos=list(-1,0, nrow(mat)), command=c(‘\\toprule ‘, ‘\\midrule ‘, ‘\\bottomrule ‘))) @ \end{table} \end{document}  To compile, Sweave needs to be run on it. Running Sweave is an pre-processing step in the latex compilation process $ R CMD Sweave table.Rnw \$ pdflatex table.tex 

and then check out your awesome new table that you didn’t type!

# Be LaTeX my friend

You may recall this quote from Bruce Lee:

Empty your mind, be formless. Shapeless, like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.

Just read it a little differently

Empty your mind of Microsoft Word, be formless. Shapeless, like a .tex file. If you put a .tex file into latex, it becomes the paper. You put a .tex file into beamer and it becomes the presentation. You put it in htlatex, it becomes a webpage. Now, LaTeX can flow or it can crash. Be LaTeX my friend.

Really what I want to focus on it the line “Now, LaTeX can flow or it can crash.” I have become slightly dogmatic in my belief that even just writing your documents in LaTeX is not enough. Your document must flow like water. What that means is little things matter. Along these lines I have come up with a list of things which can help help a document flow:

• Use the same font and font size in your graphics as in your writing. One way to do this is with eps2pgf and the -m directcopy option.